Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
using namespace std;
 
// Function to return the count
// of required characters
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++;
 
        // Two-bit character
        else
            i += 2;
 
        // Update the count
        cnt++;
    }
 
    return cnt;
}
 
// Driver code
int 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 characters
def 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 code
if __name__ == "__main__" :
 
    string = "11010";
    n = len(string);
 
    print(countChars(string, n));
     
# This code is contributed by AnkitRai01


C#




// C# implementation of the above approach
using 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




<script>
 
    // JavaScript implementation of the above approach
     
    // Function to return the count
    // of required characters
    function countChars(str, n)
    {
       
        let 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;
    }
     
    let str = "11010";
    let n = str.length;
 
    document.write(countChars(str, n));
     
</script>


Output: 

3

 

Time complexity: O(n) where n is length of given binary string
Auxiliary space: O(1) because it is using constant space for variables



Last Updated : 16 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads