# Find a string such that every character is lexicographically greater than its immediate next character in Python

Suppose we have a number n; we have to check a lower case stringof length n+1 so that the character at any position should be lexicographically bigger than its immediate next character.

So, if the input is like 15, then the output will be ponmlkjihgfedcba.

To solve this, we will follow these steps −

• temp_str := blank string
• extra := n mod 26
• if extra >= 1, then
• for i in range 26 -(extra + 1) to 25, do
• temp_str := temp_str + str[i]
• count := n / 26 (integer division)
• for i in range 1 to count + 1, do
• for j in range 0 to 25, do
• temp_str := temp_str + str[j]
• return temp_str

## Example

Let us see the following implementation to get better understanding −

Live Demo

def show_string(n, str):
temp_str = ""
extra = n % 26
if (extra >= 1) :
for i in range( 26 - (extra + 1), 26):
temp_str += str[i]
count = n // 26
for i in range(1, count + 1) :
for j in range(26):
temp_str += str[j]
return temp_str
n = 15
str = "zyxwvutsrqponmlkjihgfedcba"
print(show_string(n, str))

## Input

15

## Output

ponmlkjihgfedcba

Updated on: 28-Aug-2020

88 Views 