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++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG {
static String lexNext(String str, int n) {
char [] s = str.toCharArray();
for ( int i = n - 1 ; i >= 0 ; i--) {
if (s[i] != 'z' ) {
s[i]++;
return String.valueOf(s);
}
s[i] = 'a' ;
}
return null ;
}
static public void main(String[] args) {
String S = "abcdeg" , T = "abcfgh" ;
int n = S.length();
String res = lexNext(S, n);
if (res != T) {
System.out.println(res);
} else {
System.out.println( "-1" );
}
}
}
|
Python3
def lexNext(s, n):
for i in range (n - 1 , - 1 , - 1 ):
if s[i] ! = 'z' :
k = ord (s[i])
s[i] = chr (k + 1 )
return ''.join(s)
s[i] = 'a'
if __name__ = = "__main__" :
S = "abcdeg"
T = "abcfgh"
n = len (S)
S = list (S)
res = lexNext(S, n)
if res ! = T:
print (res)
else :
print ( - 1 )
|
C#
using System;
public class GFG {
static String lexNext(String str, int n) {
char [] s = str.ToCharArray();
for ( int i = n - 1; i >= 0; i--) {
if (s[i] != 'z' ) {
s[i]++;
return new String(s);
}
s[i] = 'a' ;
}
return null ;
}
static public void Main() {
String S = "abcdeg" , T = "abcfgh" ;
int n = S.Length;
String res = lexNext(S, n);
if (res != T) {
Console.Write(res);
} else {
Console.Write( "-1" );
}
}
}
|
Javascript
<script>
function lexNext( s, n){
for (let i = n - 1; i >= 0; i--)
{
if (s[i] != 'z' )
{
let code = s.charCodeAt(i)+1;
let str = String.fromCharCode(code);
return s.substr(0,i)+str+s.substr(i+1);
}
s[i] = 'a' ;
}
}
let S = "abcdeg" ;
let T = "abcfgh" ;
let n = S.length;
let res = lexNext(S, n);
if (res != T)
document.write( res, '<br>' );
else
document.write( "-1 <br>" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Jul, 2022
Like Article
Save Article