Check if given string is a substring of string formed by repeated concatenation of z to a | Java program for the above approach ; Function checks if a given string is valid or not and prints the output ; Boolean flag variable to mark if given string is valid ; Traverse the given string ; If adjacent character differ by 1 ; If character ' a ' is followed by 4 ; Else flip the flag and break from the loop ; Output according to flag variable ; Driver code ; Given string ; Function call
class GFG { public static void checkInfinite ( String s ) { boolean flag = true ; int N = s . length ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( s . charAt ( i ) == ( char ) ( ( int ) ( s . charAt ( i + 1 ) ) + 1 ) ) { continue ; } else if ( s . charAt ( i ) == ' a ' && s . charAt ( i + 1 ) == ' z ' ) { continue ; } else { flag = false ; break ; } } if ( ! flag ) System . out . print ( " NO " ) ; else System . out . print ( " YES " ) ; } public static void main ( String [ ] args ) { String s = " ecbaz " ; checkInfinite ( s ) ; } }