Find Kth largest string from the permutations of the string with two characters
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++
#include <bits/stdc++.h>
using namespace std;
void kthString( int n, int k)
{
int total = 0;
int i = 1;
while (total < k) {
total = total + n - i;
i++;
}
int first_y_position = i - 1;
int second_y_position = k - (total - n + first_y_position);
for ( int j = 1; j < first_y_position; j++)
cout << "x" ;
cout << "y" ;
int j = first_y_position + 1;
while (second_y_position > 1) {
cout << "x" ;
second_y_position--;
j++;
}
cout << "y" ;
while (j < n) {
cout << "x" ;
j++;
}
}
int main()
{
int n = 5;
int k = 7;
kthString(n, k);
return 0;
}
|
Java
class GFG{
static void kthString( int n, int k)
{
int total = 0 ;
int i = 1 ;
while (total < k) {
total = total + n - i;
i++;
}
int first_y_position = i - 1 ;
int second_y_position = k - (total - n + first_y_position);
for ( int j = 1 ; j < first_y_position; j++)
System.out.print( "x" );
System.out.print( "y" );
int j = first_y_position + 1 ;
while (second_y_position > 1 ) {
System.out.print( "x" );
second_y_position--;
j++;
}
System.out.print( "y" );
while (j < n) {
System.out.print( "x" );
j++;
}
}
public static void main(String[] args)
{
int n = 5 ;
int k = 7 ;
kthString(n, k);
}
}
|
Python 3
def kthString(n,k):
total = 0
i = 1
while (total < k):
total = total + n - i
i + = 1
first_y_position = i - 1
second_y_position = k - (total - n + first_y_position)
for j in range ( 1 ,first_y_position, 1 ):
print ( "x" ,end = "")
print ( "y" ,end = "")
j = first_y_position + 1
while (second_y_position > 1 ):
print ( "x" ,end = "")
second_y_position - = 1
j + = 1
print ( "y" ,end = "")
while (j < n):
print ( "x" )
j + = 1
if __name__ = = '__main__' :
n = 5
k = 7
kthString(n, k)
|
C#
using System;
class GFG{
static void kthString( int n, int k)
{
int total = 0;
int i = 1;
while (total < k) {
total = total + n - i;
i++;
}
int first_y_position = i - 1;
int second_y_position = k - (total - n + first_y_position);
int j;
for (j = 1; j < first_y_position; j++)
Console.Write( "x" );
Console.Write( "y" );
j = first_y_position + 1;
while (second_y_position > 1) {
Console.Write( "x" );
second_y_position--;
j++;
}
Console.Write( "y" );
while (j < n) {
Console.Write( "x" );
j++;
}
}
static public void Main ()
{
int n = 5;
int k = 7;
kthString(n, k);
}
}
|
Javascript
<script>
function kthString(n, k)
{
var total = 0;
var i = 1;
while (total < k) {
total = total + n - i;
i++;
}
var first_y_position = i - 1;
var second_y_position = k - (total - n + first_y_position);
for ( var j = 1; j < first_y_position; j++)
document.write( "x" );
document.write( "y" );
var j = first_y_position + 1;
while (second_y_position > 1) {
document.write( "x" );
second_y_position--;
j++;
}
document.write( "y" );
while (j < n) {
document.write( "x" );
j++;
}
}
var n = 5;
var k = 7;
kthString(n, k);
</script>
|
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
Share your thoughts in the comments
Please Login to comment...