Given a string and a limit k, find lexicographically next word which contains characters from a set of first K letters of the English alphabet and does not contain a palindrome as it’s substring of length more than one. It may be assumed that the input string does not contain a palindromic substring.
Examples:
Input : s = "cba"
k = 4
Output : cbd
Input : s = "cba"
k = 3
Output : -1
we can't form such word
Approach : Our aim is to make lexicographically next word, so, we need to move from right to left. While traversing from right to left change last alphabet from right. While incrementing the last alphabet make sure that formed string contains first k letters and does not contain any substring as a palindrome.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findNextWord(string s, int m)
{
m += 97;
int n = s.length();
int i = s.length() - 1;
s[i]++;
while (i >= 0 && i <= n - 1) {
if (s[i] >= m) {
s[i] = 'a' ;
s[--i]++;
}
else if (s[i] == s[i - 1] ||
s[i] == s[i - 2])
s[i]++;
else
i++;
}
if (i <= -1)
cout << "-1" ;
else
cout << s;
}
int main()
{
string str = "abcd" ;
int k = 4;
findNextWord(str, k);
return 0;
}
|
Java
public class GFG
{
static void findNextWord( char [] s, int m)
{
m += 97 ;
int n = s.length;
int i = s.length - 1 ;
s[i]++;
while (i >= 0 && i <= n - 1 )
{
if (s[i] >= m)
{
s[i] = 'a' ;
s[--i]++;
}
else if (s[i] == s[i - 1 ]
|| s[i] == s[i - 2 ])
{
s[i]++;
}
else
{
i++;
}
}
if (i <= - 1 )
{
System.out.println( "-1" );
}
else
{
System.out.println(s);
}
}
public static void main(String[] args)
{
char [] str = "abcd" .toCharArray();
int k = 4 ;
findNextWord(str, k);
}
}
|
Python3
def findNextWord(s, m):
m + = 97
n = len (s)
i = len (s) - 1
s[i] = chr ( ord (s[i]) + 1 )
while i > = 0 and i < = n - 1 :
if ord (s[i]) > = m:
s[i] = 'a'
i - = 1
s[i] = chr ( ord (s[i]) + 1 )
elif s[i] = = s[i - 1 ] or s[i] = = s[i - 2 ]:
s[i] = chr ( ord (s[i]) + 1 )
else :
i + = 1
if i < = - 1 :
print ( "-1" )
else :
print (''.join(s))
if __name__ = = "__main__" :
string = "abcd"
k = 4
findNextWord( list (string), k)
|
C#
using System;
class GFG
{
static void findNextWord( char [] s, int m)
{
m += 97;
int n = s.Length;
int i = s.Length - 1;
s[i]++;
while (i >= 0 && i <= n - 1)
{
if (s[i] >= m)
{
s[i] = 'a' ;
s[--i]++;
}
else if (s[i] == s[i - 1] ||
s[i] == s[i - 2])
{
s[i]++;
}
else
{
i++;
}
}
if (i <= -1)
{
Console.WriteLine( "-1" );
}
else
{
Console.WriteLine(s);
}
}
public static void Main(String[] args)
{
char [] str = "abcd" .ToCharArray();
int k = 4;
findNextWord(str, k);
}
}
|
Javascript
<script>
function findNextWord(s, m)
{
m += 97;
var n = s.length;
var i = s.length - 1;
s[i] = String.fromCharCode(s[i].charCodeAt(0)+1);
while (i >= 0 && i <= n - 1) {
if (s[i].charCodeAt(0) >= m) {
s[i] = 'a ';
s[i-1] = String.fromCharCode(s[i-1].charCodeAt(0)+1);
i--;
}
// to check whether formed string
// palindrome or not.
else if (s[i] == s[i - 1] ||
s[i] == s[i - 2])
s[i] = String.fromCharCode(s[i].charCodeAt(0)+1);
// increment i.
else
i++;
}
// if i less than or equals to one
// that means we not formed such word.
if (i <= -1)
document.write( "-1");
else
document.write( s.join(' '));
}
// Driver code for above function.
var str = "abcd".split(' ');
var k = 4;
findNextWord(str, k);
</script>
|
Time Complexity: O(N), where N represents the length of the given string.
Auxiliary Space: O(1), no extra space is required, so it is a constant.