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.
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:
- Print longest palindrome word in a sentence
- Possibility of a word from a given set of characters
- Print list items containing all characters of a given word
- Arrangement of the characters of a word such that all vowels are at odd places
- Java program to count the characters in each word in a given sentence
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Find largest word in dictionary by deleting some characters of given string
- Rearrange characters to form palindrome if possible
- Maximum length palindrome that can be created with characters in range L and R
- Check if characters of a given string can be rearranged to form a palindrome
- Print the arranged positions of characters to make palindrome
- Find the player who rearranges the characters to get a palindrome string first
- Check if a doubly linked list of characters is palindrome or not
- Make lexicographically smallest palindrome by substituting missing characters
- Check if the characters in a string form a Palindrome in O(1) extra space
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.