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

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: 3
“11”, “10” and “0” are the required characters.



Input: str = “100”
Output: 2

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3

competitive-programming-img




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01