Skip to content
Related Articles

Related Articles

Find Kth largest string from the permutations of the string with two characters
  • Last Updated : 09 Apr, 2020

Given two integers N and K, the task is find the lexicographically Kth largest string of size N from the set of strings containing only two characters ‘x’ and ‘y’, where character ‘x’ is present in the string (N – 2) times and the character ‘y’ is present only 2 times.

Examples:

Input: N = 4, K = 3
Output: yxxy
Explanation:
All the strings of size 4 –
{ xxyy, xyxy, xyyx, yxxy, yxyx, yyxx }
The 3rd smallest string will be – yxxy

Input: N = 3, K = 2
Output: yxy
Explanation:
All the strings of size 3 –
{ xyy, yxy, yyx }

Approach:
The idea is to observe that the lexicographically largest string will have 2 ‘y’ at start followed by all ‘x’. The lexicographically second largest string will have the second ‘y’ moved one index ahead that is, ‘yxyxxxxx……’ and so on.



The key observation in this problem is that character ‘y’ is present two times at the front in the lexicographically largest string and then in each step, the second character ‘y’ moves one step ahead until it reaches the end of the string to generate next smallest string. Once, the second ‘y’ reaches end of string, the next smallest string will have two ‘y’ at index 1 and 2 and then the process continues.

Therefore, the idea is to find the first and second positions for the character ‘y’ and then print these positions with ‘y’ character and all other positions filled with ‘x’ character.

Below is the implementation of the above approach:

C++




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the
// kth largest string
void kthString(int n, int k)
{
    int total = 0;
    int i = 1;
  
    // loop to iterate through
    // series
    while (total < k) {
        // total takes the position
        // of second y
        total = total + n - i;
  
        // i takes the position of
        // first y
        i++;
    }
  
    // calculating first y postion
    int first_y_position = i - 1;
  
    // calculating second y position
    // from first y
    int second_y_position = k - (total - n + first_y_position);
  
    // print all x before first y
    for (int j = 1; j < first_y_position; j++)
        cout << "x";
  
    // print first y
    cout << "y";
  
    int j = first_y_position + 1;
  
    // print all x between first y
    // and second y
    while (second_y_position > 1) {
        cout << "x";
        second_y_position--;
        j++;
    }
  
    // print second y
    cout << "y";
  
    // print x which occur
    // after second y
    while (j < n) {
        cout << "x";
        j++;
    }
}
  
// Driver code
int main()
{
    int n = 5;
  
    int k = 7;
  
    kthString(n, k);
  
    return 0;
}

Java




// Java implementation of above approach
class GFG{
   
// Function to print the
// kth largest String
static void kthString(int n, int k)
{
    int total = 0;
    int i = 1;
   
    // loop to iterate through
    // series
    while (total < k) {
        // total takes the position
        // of second y
        total = total + n - i;
   
        // i takes the position of
        // first y
        i++;
    }
   
    // calculating first y postion
    int first_y_position = i - 1;
   
    // calculating second y position
    // from first y
    int second_y_position = k - (total - n + first_y_position);
   
    // print all x before first y
    for (int j = 1; j < first_y_position; j++)
        System.out.print("x");
   
    // print first y
    System.out.print("y");
   
    int j = first_y_position + 1;
   
    // print all x between first y
    // and second y
    while (second_y_position > 1) {
        System.out.print("x");
        second_y_position--;
        j++;
    }
   
    // print second y
    System.out.print("y");
   
    // print x which occur
    // after second y
    while (j < n) {
        System.out.print("x");
        j++;
    }
}
   
// Driver code
public static void main(String[] args)
{
    int n = 5;
   
    int k = 7;
   
    kthString(n, k); 
}
}
  
// This code is contributed by sapnasingh4991

Python 3




# Python 3 implementation of above approach
  
# Function to print the
# kth largest string
def kthString(n,k):
    total = 0
    i = 1
  
    # loop to iterate through
    # series
    while (total < k):
        # total takes the position
        # of second y
        total = total + n - i
  
        # i takes the position of
        # first y
        i += 1
  
    # calculating first y postion
    first_y_position = i - 1
  
    # calculating second y position
    # from first y
    second_y_position = k - (total - n + first_y_position)
  
    # print all x before first y
    for j in range(1,first_y_position,1):
        print("x",end = "")
  
    # print first y
    print("y",end = "")
  
    j = first_y_position + 1
  
    # print all x between first y
    # and second y
    while (second_y_position > 1):
        print("x",end = "")
        second_y_position -= 1
        j += 1
  
    # print second y
    print("y",end = "")
  
    # print x which occur
    # after second y
    while (j < n):
        print("x")
        j += 1
  
# Driver code
if __name__ == '__main__':
    n = 5
    k = 7
    kthString(n, k)
  
# This code is contributed by Surendra_Gangwar

C#




// C# implementation of above approach
using System;
  
class GFG{
  
    // Function to print the
    // kth largest string
    static void kthString(int n, int k)
    {
        int total = 0;
        int i = 1;
      
        // loop to iterate through
        // series
        while (total < k) {
            // total takes the position
            // of second y
            total = total + n - i;
      
            // i takes the position of
            // first y
            i++;
        }
      
        // calculating first y postion
        int first_y_position = i - 1;
      
        // calculating second y position
        // from first y
        int second_y_position = k - (total - n + first_y_position);
          
        int j;
          
        // print all x before first y
        for (j = 1; j < first_y_position; j++)
            Console.Write("x");
      
        // print first y
        Console.Write("y");
      
        j = first_y_position + 1;
      
        // print all x between first y
        // and second y
        while (second_y_position > 1) {
            Console.Write("x");
            second_y_position--;
            j++;
        }
      
        // print second y
        Console.Write("y");
      
        // print x which occur
        // after second y
        while (j < n) {
            Console.Write("x");
            j++;
        }
    }
      
    // Driver code
    static public void Main ()
    {
        int n = 5;
  
        int k = 7;
      
        kthString(n, k);
    }
}
  
// This code is contributed by shubhamsingh10
Output:
xyxxy
My Personal Notes arrow_drop_up
Recommended Articles
Page :