Find the k-th string in lexicographical order consisting of n-2 X’s and 2 Y’s
Last Updated :
31 Jul, 2022
Given two numbers N and K, the task is to find the Kth string in lexicographical order if the starting string contains (N-2) x’s first and then 2 Y’s
Note:
1 ? K ? N*(N-1)/2, N*(N-1)/2 are the number of possible permutations
Examples:
Input : N = 5, K = 7
Output : YXXXY
The possible strings in lexicographical order
1. XXXYY
2. XXYXY
3. XXYYX
4. XYXXY
5. XYXYX
6. XYYXX
7. YXXXY
8. YXXYX
9. YXYXX
10. YYXXX
Input : N = 8, K = 20
Output : XYXYXXXX
Approach:
Inorder to find the kth position string, we have to follow the following steps-
- we have to find the position of the leftmost occurrence of ‘Y’ by iterating over all positions from n-2 to 0.
- Now while iterating, if k<=n-i-1 then this is the required position of the leftmost occurrence of ‘Y’ and the position of rightmost occurrence is n-k so we can print the answer.
- Otherwise, let’s decrease k by n-i-1, i.e, remove all strings which have the leftmost ‘Y’ at the current position and proceed to the next position. In this way we consider all possible strings in lexicographic order.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void kth_string( int n, int k)
{
for ( int i = n - 2; i >= 0; i--) {
if (k <= (n - i - 1)) {
for ( int j = 0; j < n; j++) {
if (j == i or j == n - k)
cout << 'Y' ;
else
cout << 'X' ;
}
break ;
}
k -= (n - i - 1);
}
}
int main()
{
int n = 5, k = 7;
kth_string(n, k);
return 0;
}
|
Java
class GFG{
static void kth_String( int n, int k)
{
for ( int i = n - 2 ; i >= 0 ; i--)
{
if (k <= (n - i - 1 ))
{
for ( int j = 0 ; j < n; j++)
{
if (j == i || j == n - k)
System.out.print( 'Y' );
else
System.out.print( 'X' );
}
break ;
}
k -= (n - i - 1 );
}
}
public static void main(String[] args)
{
int n = 5 , k = 7 ;
kth_String(n, k);
}
}
|
Python3
def kth_string(n, k):
for i in range (n - 2 , - 1 , - 1 ):
if k < = (n - i - 1 ):
for j in range (n):
if (j = = i or j = = n - k):
print ( 'Y' , end = "")
else :
print ( 'X' , end = "")
break
k - = (n - i - 1 )
n = 5
k = 7
kth_string(n, k)
|
C#
using System;
class GFG{
static void kth_String( int n, int k)
{
for ( int i = n - 2; i >= 0; i--)
{
if (k <= (n - i - 1))
{
for ( int j = 0; j < n; j++)
{
if (j == i || j == n - k)
Console.Write( 'Y' );
else
Console.Write( 'X' );
}
break ;
}
k -= (n - i - 1);
}
}
public static void Main(String[] args)
{
int n = 5, k = 7;
kth_String(n, k);
}
}
|
Javascript
<script>
function kth_String(n , k)
{
for (i = n - 2; i >= 0; i--)
{
if (k <= (n - i - 1))
{
for (j = 0; j < n; j++)
{
if (j == i || j == n - k)
document.write( 'Y' );
else
document.write( 'X' );
}
break ;
}
k -= (n - i - 1);
}
}
var n = 5, k = 7;
kth_String(n, k);
</script>
|
Time Complexity: O(N2) // two loops are used nested inside each other so the complexity is n*n
Auxiliary Space: O (1) // no extra array is used so constant space
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...