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.
- Lexicographically next greater string using same character set
- 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 smallest string formed by removing at most one character
- Replace every character of string by character whose ASCII value is K times more than it
- Modify the string such that every character gets replaced with the next character in the keyboard
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace every character of a string by a different character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Find the character in first string that is present at minimum index in second string
- Find last index of a character in a string
- Given a string, find its first non-repeating character
- Find k'th character of decrypted string | Set 1
- Find the last non repeating character in string
- Find the first repeated character in a string