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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 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
- Find if string is K-Palindrome or not using all characters exactly once
- 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
- Find the player who rearranges the characters to get a palindrome string first
- Maximum length palindrome that can be created with characters in range L and R
- Print the arranged positions of characters to make palindrome
- Check if string remains palindrome after removing given number of characters
- Minimum number of characters to be replaced to make a given string Palindrome
- Make lexicographically smallest palindrome by substituting missing characters
- Minimum characters to be added at front to make string palindrome
- Check if the characters in a string form a Palindrome in O(1) extra space
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
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.