Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1' | Java program for the above approach ; Function to count the strings consisting of at least 1 set bit ; Initialize count ; Iterate through string ; The answer is 2 ^ N - 1 ; Driver Code ; Given string ; Function call
import java . util . * ; class GFG { static void countString ( String S ) { int count = 0 ; for ( char it : S . toCharArray ( ) ) { if ( it == '0' && count > 0 ) { count -- ; } else { count ++ ; } } System . out . print ( ( 1 << count ) - 1 ) ; } public static void main ( String [ ] args ) { String S = "1001" ; countString ( S ) ; } }