Lexicographically next string | Javascript 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
function nextWord ( s ) { if ( s == " " ) return " " ; var i = s . length - 1 ; while ( s [ i ] == ' ' && i >= 0 ) i -- ; if ( i == - 1 ) s = s + ' ' ; else s [ i ] = String . fromCharCode ( s [ i ] . charCodeAt ( 0 ) + 1 ) ; return s . join ( ' ' ) ; } var str = " " . split ( ' ' ) ; document . write ( nextWord ( str ) ) ;