Next word that does not contain a palindrome and has characters from first k

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find lexicographically
// next word which contains first K 
// letters of the English alphabet 
// and does not contain a palindrome 
// as it's substring of length more
// than one.
#include <bits/stdc++.h>
using namespace std;
  
// function to return lexicographically
// next word 
void findNextWord(string s, int m)
{
    // we made m as m+97 that means 
    // our required string contains
    // not more than m+97(as per ASCII 
    // value) in it.
    m += 97;
    int n = s.length();
    int i = s.length() - 1;
      
    // increment last alphabet to make
    // next lexicographically next word.
    s[i]++;
  
    while (i >= 0 && i <= n - 1) {
          
        // if i-th alphabet not in first
        // k letters then make it as "a"
        // and then increase (i-1)th letter
        if (s[i] >= m) {
            s[i] = 'a';
            s[--i]++;
        }
  
        // to check whether formed string 
        // palindrome or not.
        else if (s[i] == s[i - 1] || 
                 s[i] == s[i - 2]) 
            s[i]++;
  
        // increment i.
        else
            i++;
    }
  
    // if i less than or equals to one
    // that means we not formed such word.
    if (i <= -1)
        cout << "-1";
    else
        cout << s;
}
  
// Driver code for above function.
int main()
{
    string str = "abcd";
    int k = 4;
    findNextWord(str, k);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find lexicographically
// next word which contains first K 
// letters of the English alphabet 
// and does not contain a palindrome 
// as it's substring of length more
// than one.
  
public class GFG 
{
  
    // function to return lexicographically
    // next word 
    static void findNextWord(char[] s, int m) 
    {
        // we made m as m+97 that means 
        // our required string contains
        // not more than m+97(as per ASCII 
        // value) in it.
        m += 97;
        int n = s.length;
        int i = s.length - 1;
  
        // increment last alphabet to make
        // next lexicographically next word.
        s[i]++;
  
        while (i >= 0 && i <= n - 1
        {
  
            // if i-th alphabet not in first
            // k letters then make it as "a"
            // and then increase (i-1)th letter
            if (s[i] >= m)
            {
                s[i] = 'a';
                s[--i]++;
            
            // to check whether formed string 
            // palindrome or not.
            else if (s[i] == s[i - 1]
                    || s[i] == s[i - 2]) 
            {
                s[i]++;
            
            // increment i.
            else
            {
                i++;
            }
        }
  
        // if i less than or equals to one
        // that means we not formed such word.
        if (i <= -1)
        {
            System.out.println("-1");
        }
        else
        {
            System.out.println(s);
        }
    }
  
    // Driver code 
    public static void main(String[] args) 
    {
        char[] str = "abcd".toCharArray();
        int k = 4;
        findNextWord(str, k);
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find lexicographically 
# next word which contains first K 
# letters of the English alphabet and 
# does not contain a palindrome as it's
# substring of length more than one. 
  
# Function to return lexicographically next word 
def findNextWord(s, m): 
  
    # we made m as m+97 that means 
    # our required string contains 
    # not more than m+97(as per ASCII 
    # value) in it. 
    m += 97
    n = len(s) 
    i = len(s) - 1
      
    # increment last alphabet to make 
    # next lexicographically next word. 
    s[i] = chr(ord(s[i]) + 1)
  
    while i >= 0 and i <= n - 1
          
        # if i-th alphabet not in first 
        # k letters then make it as "a" 
        # and then increase (i-1)th letter 
        if ord(s[i]) >= m:
            s[i] = 'a'
            i -= 1
            s[i] = chr(ord(s[i]) + 1
  
        # to check whether formed string 
        # palindrome or not. 
        elif s[i] == s[i - 1] or s[i] == s[i - 2]: 
            s[i] = chr(ord(s[i]) + 1
  
        # increment i. 
        else:
            i += 1
      
    # if i less than or equals to one 
    # that means we not formed such word. 
    if i <= -1:
        print("-1"
    else:
        print(''.join(s)) 
  
# Driver code 
if __name__ == "__main__"
  
    string = "abcd"
    k = 4
    findNextWord(list(string), k) 
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find lexicographically
// next word which contains first K 
// letters of the English alphabet 
// and does not contain a palindrome 
// as it's substring of length more
// than one.
using System;
  
class GFG 
{
  
    // function to return lexicographically
    // next word 
    static void findNextWord(char[] s, int m) 
    {
        // we made m as m+97 that means 
        // our required string contains
        // not more than m+97(as per ASCII 
        // value) in it.
        m += 97;
        int n = s.Length;
        int i = s.Length - 1;
  
        // increment last alphabet to make
        // next lexicographically next word.
        s[i]++;
  
        while (i >= 0 && i <= n - 1) 
        {
  
            // if i-th alphabet not in first
            // k letters then make it as "a"
            // and then increase (i-1)th letter
            if (s[i] >= m)
            {
                s[i] = 'a';
                s[--i]++;
            
            // to check whether formed string 
            // palindrome or not.
            else if (s[i] == s[i - 1] ||
                     s[i] == s[i - 2]) 
            {
                s[i]++;
            
              
            // increment i.
            else
            {
                i++;
            }
        }
  
        // if i less than or equals to one
        // that means we not formed such word.
        if (i <= -1)
        {
            Console.WriteLine("-1");
        }
        else
        {
            Console.WriteLine(s);
        }
    }
  
    // Driver code 
    public static void Main(String[] args) 
    {
        char[] str = "abcd".ToCharArray();
        int k = 4;
        findNextWord(str, k);
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

abda


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.