Given two strings S and T, find a string of the same length which is lexicographically greater than S and smaller than T. Print “-1” if no such string is formed.(S > T)
Note: string S = s1s2… sn is said to be lexicographically smaller than string T = t1t2… tn, if there exists an i, such that s1 = t1, s2 = t2, … si – 1 = ti – 1, si < ti.
Input : S = "aaa", T = "ccc"
Output : aab
Here, 'b' is greater than any
letter in S('a') and smaller
than any letter in T('c').
Input : S = "abcde", T = "abcdf"
Output : -1
There is no other string between
S and T.
Approach: Find a string which is lexicographically greater than string S and check if it is smaller than string T, if yes print the string next else print “-1”. To find string, iterate the string S in the reverse order, if the last letter is not ‘z’, increase the letter by one (to move to next letter). If it is ‘z’, change it to ‘a’ and move to the second last character. Compare the resultant string with string T, if both strings are equal print ‘-1’, else print the resultant string.