Count of 1-bit and 2-bit characters in the given binary string

• Difficulty Level : Medium
• Last Updated : 10 Jun, 2021

Given two special characters, the first character can be represented by one bit which is 0 and the second character can be represented by two bits either 10 or 11. Now given a string represented by several bits. The task is to return the number of characters it represents. Note that the given string is always valid.
Examples:

Input: str = “11100”
Output:
“11”, “10” and “0” are the required characters.
Input: str = “100”
Output:

Approach: The approach to solve the problem is that if the current character is 0 then it represents a single character of 1 bit but if the current character is 1 then the next bit after it has to be included in the character consisting of two bits as there is no single bit characters starting with 1.
Below is the implementation of the above approach:

C++

 // C++ implementation of the approach#include using namespace std; // Function to return the count// of required charactersint countChars(string str, int n){     int i = 0, cnt = 0;     // While there are characters left    while (i < n) {         // Single bit character        if (str[i] == '0')            i++;         // Two-bit character        else            i += 2;         // Update the count        cnt++;    }     return cnt;} // Driver codeint main(){    string str = "11010";    int n = str.length();     cout << countChars(str, n);     return 0;}

Java

 // Java implementation of the above approach class GFG {         // Function to return the count    // of required characters    static int countChars(String str, int n)    {             int i = 0, cnt = 0;             // While there are characters left        while (i < n) {                 // Single bit character            if (str.charAt(i) == '0')                i += 1;                 // Two-bit character            else                i += 2;                 // Update the count            cnt += 1;        }             return cnt;    }         // Driver code    public static void main (String[] args)    {        String str = "11010";        int n = str.length();             System.out.println(countChars(str, n));    }    // This code is contributed by AnkitRai01}

Python3

 # Python3 implementation of the approach # Function to return the count# of required charactersdef countChars(string, n) :     i = 0; cnt = 0;     # While there are characters left    while (i < n) :         # Single bit character        if (string[i] == '0'):            i += 1;         # Two-bit character        else :            i += 2;         # Update the count        cnt += 1;     return cnt; # Driver codeif __name__ == "__main__" :     string = "11010";    n = len(string);     print(countChars(string, n));     # This code is contributed by AnkitRai01

C#

 // C# implementation of the above approachusing System; class GFG{         // Function to return the count    // of required characters    static int countChars(string str, int n)    {             int i = 0, cnt = 0;             // While there are characters left        while (i < n)        {                 // Single bit character            if (str[i] == '0')                i += 1;                 // Two-bit character            else                i += 2;                 // Update the count            cnt += 1;        }             return cnt;    }         // Driver code    public static void Main ()    {        string str = "11010";        int n = str.Length;             Console.WriteLine(countChars(str, n));    }} // This code is contributed by AnkitRai01

Javascript


Output:
3

My Personal Notes arrow_drop_up