Find a string in lexicographic order which is in between given two strings | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexNext ( string s , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != ' z ' ) { s [ i ] ++ ; return s ; } s [ i ] = ' a ' ; } } int main ( ) { string S = " abcdeg " , T = " abcfgh " ; int n = S . length ( ) ; string res = lexNext ( S , n ) ; if ( res != T ) cout << res << endl ; else cout << " - 1" << endl ; return 0 ; }