Find a string in lexicographic order which is in between given two strings | Java program to find the string in lexicographic order which is in between given two strings ; Function to find the lexicographically next string ; Iterate from last character ; If not ' z ' , increase by one ; if ' z ' , change it to ' a ' ; Driver Code ; If not equal , print the resultant string
class GFG { static String lexNext ( String str , int n ) { char [ ] s = str . toCharArray ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != ' z ' ) { s [ i ] ++ ; return String . valueOf ( s ) ; } s [ i ] = ' a ' ; } return null ; } static public void main ( String [ ] args ) { String S = " abcdeg " , T = " abcfgh " ; int n = S . length ( ) ; String res = lexNext ( S , n ) ; if ( res != T ) { System . out . println ( res ) ; } else { System . out . println ( " - 1" ) ; } } }