
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find a String with Lexicographically Greater Characters 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]
- for j in range 0 to 25, do
- for i in range 26 -(extra + 1) to 25, do
- return temp_str
Example
Let us see the following implementation to get better understanding −
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
Advertisements