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 Explanation: 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 Explanation: 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.
Below is the implementation of above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generating distinct subsequences of a given string in lexicographic order
- Print a number as string of 'A' and 'B' in lexicographic order
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard
- Print all permutations in sorted (lexicographic) order
- Power Set in Lexicographic order
- Lexicographic smallest permutation of a String containing the second String as a Substring
- Find the N-th lexicographic permutation of string using Factoradic method
- Lexicographic rank of a string
- Lexicographic rank of a string using STL
- Lexicographic rank of a string among all its substrings
- Lexicographic rank of a string with duplicate characters
- Longest string which is prefix string of at least two strings
- Largest lexicographic array with at-most K consecutive swaps
- Given two strings check which string makes a palindrome first
- Count of same length Strings that exists lexicographically in between two given Strings
- Concatenate the strings in an order which maximises the occurrence of subsequence "ab"
- Meta Strings (Check if two strings can become same after a swap in one string)
- Longest palindromic String formed using concatenation of given strings in any order
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Number of sub-strings which are anagram of any sub-string of another string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.