Lexicographically next string | Java program to find lexicographically next string ; if string is empty ; Find first character from right which is not z . ; If all characters are ' z ' , append an ' a ' at the end . ; If there are some non - z characters ; Driver Code
import java . util . * ; class GFG { public static String nextWord ( String str ) { if ( str == " " ) return " a " ; int i = str . length ( ) - 1 ; while ( str . charAt ( i ) == ' z ' && i >= 0 ) i -- ; if ( i == - 1 ) str = str + ' a ' ; else str = str . substring ( 0 , i ) + ( char ) ( ( int ) ( str . charAt ( i ) ) + 1 ) + str . substring ( i + 1 ) ; return str ; } public static void main ( String [ ] args ) { String str = " samez " ; System . out . print ( nextWord ( str ) ) ; } }