Given an integer N, the task is to find a string(considering only lowercase characters) of length (N+1) such that the character at any position should be lexicographically greater than its immediate next character.
Input: 2 Output: cba c is greater than b and b is greater than a Input: 5 Output: fedcba
- Declare a string with all the alphabets in reverse order.
- Take modulus of the given number with 26. So, if the value is less than 26, run a loop from 26 – (Modulus Value + 1) to 25 and go to that index of the string and print that index.
- Divide the modulus value with 26 if value comes greater than 0 then run the loop to 0 to 25 and print every element of the string by given the calculated value.
Below is the implementation of above approach:
This article is contributed by Sahil Rajput. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Lexicographically next greater string using same character set
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace consonants with next immediate consonants alphabetically in a String
- Replace every vowels with lexicographically next vowel in a String
- Lexicographically largest subsequence such that every character occurs at least k times
- Replace every character of string by character whose ASCII value is K times more than it
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Map every character of one string to another such that all occurrences are mapped to the same character
- Lexicographically next string
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum string such that every adjacent character of given string is still adjacent
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically next permutation in C++
- Replace every character of a string by a different character
- Count of alphabets having ASCII value less than and greater than k
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count the number of words having sum of ASCII values less than and greater than k
- Lexicographically smallest string formed by removing at most one character