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
- Arrangement of the characters of a word such that all vowels are at odd places
- Print list items containing all characters of a given word
- Java program to count the characters in each word in a given sentence
- Find largest word in dictionary by deleting some characters of given string
- Find the count of sub-strings whose characters can be rearranged to form the given word
- 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
- Check if a doubly linked list of characters is palindrome or not
- Print the arranged positions of characters to make palindrome
- Find the player who rearranges the characters to get a palindrome string first
- Minimum characters to be added at front to make string palindrome
- Minimum number of characters to be replaced to make a given string Palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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 : rituraj_jain