Find a string in lexicographic order which is in between given two strings
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.
Examples:
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:
C++
// CPP program to find the string // in lexicographic order which is // in between given two strings #include <bits/stdc++.h> using namespace std; // Function to find the lexicographically // next string string lexNext(string s, int n) { // Iterate from last character for (int i = n - 1; i >= 0; i--) { // If not 'z', increase by one if (s[i] != 'z') { s[i]++; return s; } // if 'z', change it to 'a' s[i] = 'a'; } } // Driver Code int main() { string S = "abcdeg", T = "abcfgh"; int n = S.length(); string res = lexNext(S, n); // If not equal, print the // resultant string if (res != T) cout << res << endl; else cout << "-1" << endl; return 0; } |
Java
//Java program to find the string // in lexicographic order which is // in between given two strings class GFG { // Function to find the lexicographically // next string static String lexNext(String str, int n) { char[] s = str.toCharArray(); // Iterate from last character for (int i = n - 1; i >= 0; i--) { // If not 'z', increase by one if (s[i] != 'z') { s[i]++; return String.valueOf(s); } // if 'z', change it to 'a' s[i] = 'a'; } return null; } // Driver Code static public void main(String[] args) { String S = "abcdeg", T = "abcfgh"; int n = S.length(); String res = lexNext(S, n); // If not equal, print the // resultant string if (res != T) { System.out.println(res); } else { System.out.println("-1"); } } } // This code is contributed by 29AjayKumar |
C#
//C# program to find the string // in lexicographic order which is // in between given two strings using System; public class GFG { // Function to find the lexicographically // next string static String lexNext(String str, int n) { char[] s = str.ToCharArray(); // Iterate from last character for (int i = n - 1; i >= 0; i--) { // If not 'z', increase by one if (s[i] != 'z') { s[i]++; return new String(s); } // if 'z', change it to 'a' s[i] = 'a'; } return null; } // Driver Code static public void Main() { String S = "abcdeg", T = "abcfgh"; int n = S.Length; String res = lexNext(S, n); // If not equal, print the // resultant string if (res != T) { Console.Write(res); } else { Console.Write("-1"); } } } // This code is contributed by 29AjayKumar |
Output:
abcdeh
Recommended Posts:
- Print a number as string of 'A' and 'B' in lexicographic order
- Generating distinct subsequences of a given string in lexicographic order
- Power Set in Lexicographic order
- Print all permutations in sorted (lexicographic) order
- Print array of strings in sorted order without copying one string into another
- Lexicographic rank of a string
- Lexicographic rank of a string using STL
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Lexicographic rank of a string with duplicate characters
- Find if a string is interleaved of two other strings | DP-33
- Find all palindromic sub-strings of a given string | Set 2
- Given two strings, find if first string is a subsequence of second
- Find all distinct palindromic sub-strings of a given string
- Find the longest string that can be made up of other strings from the array
- Meta Strings (Check if two strings can become same after a swap in one 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 contribute@geeksforgeeks.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.
Improved By : 29AjayKumar



