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