Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 

  1. Declare a string with all the alphabets in reverse order.
  2. 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.
  3. 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++




// C++ program to print a string in reverse
// alphabetical order upto given number
#include <bits/stdc++.h>
using namespace std;
 
// Function that prints the required string
string printString(int n, string str)
{
    string str2 = "";
 
    // Find modulus with 26
    int extraChar = n % 26;
 
    // Print extra characters required
    if (extraChar >= 1) {
        for (int i = 26 - (extraChar + 1); i <= 25; i++)
            str2 += str[i];
    }
    int countOfStr = n / 26;
 
    // Print the given reverse string countOfStr times
    for (int i = 1; i <= countOfStr; i++) {
        for (int j = 0; j < 26; j++)
            str2 += str[j];
    }
    return str2;
}
 
// Driver Code
int main()
{
    int n = 30;
 
    // Initialize a string in reverse order
    string str = "zyxwvutsrqponmlkjihgfedcba";
 
    cout << printString(n, str);
 
    return 0;
}


Java




// Java program to print a String in reverse
// alphabetical order upto given number
 
class GFG {
 
// Function that prints the required String
    static String printString(int n, String str) {
        String str2 = "";
 
        // Find modulus with 26
        int extraChar = n % 26;
 
        // Print extra characters required
        if (extraChar >= 1) {
            for (int i = 26 - (extraChar + 1); i <= 25; i++) {
                str2 += str.charAt(i);
            }
        }
        int countOfStr = n / 26;
 
        // Print the given reverse String countOfStr times
        for (int i = 1; i <= countOfStr; i++) {
            for (int j = 0; j < 26; j++) {
                str2 += str.charAt(j);
            }
        }
        return str2;
    }
 
// Driver Code
    public static void main(String[] args) {
        int n = 30;
 
        // Initialize a String in reverse order
        String str = "zyxwvutsrqponmlkjihgfedcba";
        System.out.println(printString(n, str));
    }
}
 
// This code is contributed by Rajput-JI


Python 3




# Python 3 program to print a
# string in reverse alphabetical
# order upto given number
 
# Function that prints the
# required string
def printString(n, str):
 
    str2 = ""
 
    # Find modulus with 26
    extraChar = n % 26
 
    # Print extra characters required
    if (extraChar >= 1) :
        for i in range( 26 - (extraChar + 1), 26):
            str2 += str[i]
 
    countOfStr = n // 26
 
    # Print the given reverse
    # string countOfStr times
    for i in range(1, countOfStr + 1) :
        for j in range(26):
            str2 += str[j]
    return str2
 
# Driver Code
if __name__ == "__main__":
    n = 30
 
    # Initialize a string in
    # reverse order
    str = "zyxwvutsrqponmlkjihgfedcba"
 
    print(printString(n, str))
 
# This code is contributed
# by ChitraNayal


C#




// C# program to print a String in reverse
// alphabetical order upto given number
using System;
public class GFG {
 
// Function that prints the required String
    static String printString(int n, String str) {
        String str2 = "";
 
        // Find modulus with 26
        int extraChar = n % 26;
 
        // Print extra characters required
        if (extraChar >= 1) {
            for (int i = 26 - (extraChar + 1); i <= 25; i++) {
                str2 += str[i];
            }
        }
        int countOfStr = n / 26;
 
        // Print the given reverse String countOfStr times
        for (int i = 1; i <= countOfStr; i++) {
            for (int j = 0; j < 26; j++) {
                str2 += str[j];
            }
        }
        return str2;
    }
 
// Driver Code
    public static void Main() {
        int n = 30;
 
        // Initialize a String in reverse order
        String str = "zyxwvutsrqponmlkjihgfedcba";
        Console.Write(printString(n, str));
    }
}
 
// This code is contributed by Rajput-JI


Javascript




<script>
      // JavaScript program to print a string in reverse
      // alphabetical order upto given number
 
      // Function that prints the required string
      function printString(n, str) {
        var str2 = "";
 
        // Find modulus with 26
        var extraChar = n % 26;
 
        // Print extra characters required
        if (extraChar >= 1) {
          for (var i = 26 - (extraChar + 1); i <= 25; i++) str2 += str[i];
        }
        var countOfStr = parseInt(n / 26);
 
        // Print the given reverse string countOfStr times
        for (var i = 1; i <= countOfStr; i++) {
          for (var j = 0; j < 26; j++) str2 += str[j];
        }
        return str2;
      }
 
      // Driver Code
      var n = 30;
       
      // Initialize a string in reverse order
      var str = "zyxwvutsrqponmlkjihgfedcba";
      document.write(printString(n, str));
       
      // This code is contributed by rdtank.
    </script>


Output

edcbazyxwvutsrqponmlkjihgfedcba

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(n)


Last Updated : 26 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads