Open In App

Find Kth largest string from the permutations of the string with two characters

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers N and K, the task is to 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 position
    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 position
    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 position
    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 position
        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


Javascript




<script>
 
// Javascript implementation of above approach
 
// Function to print the
// kth largest string
function kthString(n, k)
{
    var total = 0;
    var 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 position
    var first_y_position = i - 1;
 
    // calculating second y position
    // from first y
    var second_y_position = k - (total - n + first_y_position);
 
    // print all x before first y
    for (var j = 1; j < first_y_position; j++)
        document.write( "x");
 
    // print first y
    document.write( "y");
 
    var j = first_y_position + 1;
 
    // print all x between first y
    // and second y
    while (second_y_position > 1) {
        document.write( "x");
        second_y_position--;
        j++;
    }
 
    // print second y
    document.write( "y");
 
    // print x which occur
    // after second y
    while (j < n) {
        document.write( "x");
        j++;
    }
}
 
// Driver code
var n = 5;
var k = 7;
kthString(n, k);
 
// This code is contributed by rutvik_56.
</script>


Output: 

xyxxy

 

Time Complexity: O(N + K), where N is the size of the given string and K is the given integer.
Auxiliary Space: O(1), as constant space is required.



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