Check if given Binary string follows then given condition or not | Java program for the above approach ; Function to check if the String follows rules or not ; Check for the first condition ; Check for the third condition ; Check for the second condition ; Driver Code ; Given String str ; Function call
import java . util . * ; class GFG { static boolean checkrules ( String s ) { if ( s . length ( ) == 0 ) return true ; if ( s . charAt ( 0 ) != '1' ) return false ; if ( s . length ( ) > 2 ) { if ( s . charAt ( 1 ) == '0' && s . charAt ( 2 ) == '0' ) return checkrules ( s . substring ( 3 ) ) ; } return checkrules ( s . substring ( 1 ) ) ; } public static void main ( String [ ] args ) { String str = "1111" ; if ( checkrules ( str ) ) { System . out . print ( " Valid ▁ String " ) ; } else { System . out . print ( " Invalid ▁ String " ) ; } } }