Find a string such that every character is lexicographically greater than its immediate next character
Last Updated :
26 Aug, 2022
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.
Examples:
Input: 2
Output: cba
c is greater than b and
b is greater than a
Input: 5
Output: fedcba
Approach: 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
- 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 comesGiven 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 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:
C++
#include <bits/stdc++.h>
using namespace std;
string printString( int n, string str)
{
string str2 = "" ;
int extraChar = n % 26;
if (extraChar >= 1) {
for ( int i = 26 - (extraChar + 1); i <= 25; i++)
str2 += str[i];
}
int countOfStr = n / 26;
for ( int i = 1; i <= countOfStr; i++) {
for ( int j = 0; j < 26; j++)
str2 += str[j];
}
return str2;
}
int main()
{
int n = 30;
string str = "zyxwvutsrqponmlkjihgfedcba" ;
cout << printString(n, str);
return 0;
}
|
Java
class GFG {
static String printString( int n, String str) {
String str2 = "" ;
int extraChar = n % 26 ;
if (extraChar >= 1 ) {
for ( int i = 26 - (extraChar + 1 ); i <= 25 ; i++) {
str2 += str.charAt(i);
}
}
int countOfStr = n / 26 ;
for ( int i = 1 ; i <= countOfStr; i++) {
for ( int j = 0 ; j < 26 ; j++) {
str2 += str.charAt(j);
}
}
return str2;
}
public static void main(String[] args) {
int n = 30 ;
String str = "zyxwvutsrqponmlkjihgfedcba" ;
System.out.println(printString(n, str));
}
}
|
Python 3
def printString(n, str ):
str2 = ""
extraChar = n % 26
if (extraChar > = 1 ) :
for i in range ( 26 - (extraChar + 1 ), 26 ):
str2 + = str [i]
countOfStr = n / / 26
for i in range ( 1 , countOfStr + 1 ) :
for j in range ( 26 ):
str2 + = str [j]
return str2
if __name__ = = "__main__" :
n = 30
str = "zyxwvutsrqponmlkjihgfedcba"
print (printString(n, str ))
|
C#
using System;
public class GFG {
static String printString( int n, String str) {
String str2 = "" ;
int extraChar = n % 26;
if (extraChar >= 1) {
for ( int i = 26 - (extraChar + 1); i <= 25; i++) {
str2 += str[i];
}
}
int countOfStr = n / 26;
for ( int i = 1; i <= countOfStr; i++) {
for ( int j = 0; j < 26; j++) {
str2 += str[j];
}
}
return str2;
}
public static void Main() {
int n = 30;
String str = "zyxwvutsrqponmlkjihgfedcba" ;
Console.Write(printString(n, str));
}
}
|
Javascript
<script>
function printString(n, str) {
var str2 = "" ;
var extraChar = n % 26;
if (extraChar >= 1) {
for ( var i = 26 - (extraChar + 1); i <= 25; i++) str2 += str[i];
}
var countOfStr = parseInt(n / 26);
for ( var i = 1; i <= countOfStr; i++) {
for ( var j = 0; j < 26; j++) str2 += str[j];
}
return str2;
}
var n = 30;
var str = "zyxwvutsrqponmlkjihgfedcba" ;
document.write(printString(n, str));
</script>
|
Output
edcbazyxwvutsrqponmlkjihgfedcba
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...