Count of 1-bit and 2-bit characters in the given binary string Last Updated : 16 Nov, 2022 Summarize Comments Improve Suggest changes Share Like Article Like 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: 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++ // 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 stringAuxiliary space: O(1) because it is using constant space for variables Comment More infoAdvertise with us Next Article Count of groups of consecutive 1s in a given Binary String V vaishalichauhan3003 Follow Improve Article Tags : DSA Technical Scripter 2019 binary-string Similar Reads Count of substrings of a given Binary string with all characters same Given binary string str containing only 0 and 1, the task is to find the number of sub-strings containing only 1s and 0s respectively, i.e all characters same. Examples: Input: str = â011âOutput: 4Explanation: Three sub-strings are "1", "1", "11" which have only 1 in them, and one substring is there 10 min read Count of groups of consecutive 1s in a given Binary String Given a binary string S of size N, the task is to find the number of groups of 1s only in the string S. Examples: Input: S = "100110111", N = 9Output: 3Explanation: The following groups are of 1s only: Group over the range [0, 0] which is equal to "1".Group over the range [3, 4] which is equal to "1 5 min read Count permutations possible by replacing '?' characters in a Binary String Given a string S consisting of characters 0, 1, and '?', the task is to count all possible combinations of the binary string formed by replacing '?' by 0 or 1. Examples: Input: S = "0100?110"Output: 2Explanation: Replacing each '?'s with '1' and '0', the count of such strings formed will be equal to 3 min read Count of binary string of length N with X 0s and Y 1s Given positive integers N, X and Y. The task is to find the count of unique binary strings of length N having X 0s and Y 1s. Examples: Input: N=5, X=3, Y=2Output: 10Explanation: There are 10 binary strings of length 5 with 3 0s and 2 1s, such as: 00011, 00101, 01001, 10001, 00110, 01010, 10010, 0110 4 min read Count of substrings that start and end with 1 in given Binary String Given a binary string s, the task is to count all substrings that start and end with the character '1'. A valid substring must have both its first and last characters as '1', and can include one or more number of characters in between.Examples:Input: s = "00100101"Output: 3Explanation: Valid substri 7 min read Count the Number of matching characters in a pair of strings Given a pair of non-empty strings str1 and str2, the task is to count the number of matching characters in these strings. Consider the single count for the character which have duplicates in the strings.Examples: Input: str1 = "abcdef", str2 = "defghia" Output: 4 Matching characters are: a, d, e, fI 7 min read Like