给定两个数字N和K ,如果起始字符串包含(N-2) 个 x和2 个 Y ,则任务是按字典顺序找到第K个字符串
笔记:
1 ≤ K ≤ N*(N-1)/2, N*(N-1)/2 are the number of possible permutations
例子:
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
方法:
为了找到第k个位置的字符串,我们必须遵循以下步骤——
- 我们必须通过迭代从n-2到0 的所有位置来找到最左边出现的‘Y’的位置。
- 现在在迭代时,如果k<=ni-1那么这是‘Y’最左边出现的所需位置,最右边出现的位置是nk,所以我们可以打印答案。
- 否则,让我们将k减少ni-1 ,即删除所有在当前位置最左边的‘Y’ 的字符串并继续到下一个位置。通过这种方式,我们按字典顺序考虑所有可能的字符串。
下面是上述方法的实现。
C++
// C++ program find the Kth string in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
#include
using namespace std;
// Function to find the Kth string
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
void kth_string(int n, int k)
{
// Iterate for all possible positions of
// Left most Y
for (int i = n - 2; i >= 0; i--) {
// If i is the left most position of Y
if (k <= (n - i - 1)) {
// Put Y in their positions
for (int j = 0; j < n; j++) {
if (j == i or j == n - k)
cout << 'Y';
else
cout << 'X';
}
break;
}
k -= (n - i - 1);
}
}
// Driver code
int main()
{
int n = 5, k = 7;
// Function call
kth_string(n, k);
return 0;
}
Java
// Java program find the Kth String in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
class GFG{
// Function to find the Kth String
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
static void kth_String(int n, int k)
{
// Iterate for all possible
// positions of eft most Y
for(int i = n - 2; i >= 0; i--)
{
// If i is the left
// most position of Y
if (k <= (n - i - 1))
{
// Put Y in their positions
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);
}
}
// Driver code
public static void main(String[] args)
{
int n = 5, k = 7;
// Function call
kth_String(n, k);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program find the Kth string in
# lexicographical order consisting
# of N-2 x’s and 2 y’s
# Function to find the Kth string
# in lexicographical order which
# consists of N-2 x’s and 2 y’s
def kth_string(n, k):
# Iterate for all possible positions of
# left most Y
for i in range(n - 2, -1, -1):
# If i is the left most position of Y
if k <= (n - i - 1):
# Put Y in their positions
for j in range(n):
if (j == i or j == n - k):
print('Y', end = "")
else:
print('X', end = "")
break
k -= (n - i - 1)
# Driver code
n = 5
k = 7
# Function call
kth_string(n, k)
# This code is contributed by divyamohan123
C#
// C# program find the Kth String in
// lexicographical order consisting
// of N-2 x’s and 2 y’s
using System;
class GFG{
// Function to find the Kth String
// in lexicographical order which
// consists of N-2 x’s and 2 y’s
static void kth_String(int n, int k)
{
// Iterate for all possible
// positions of eft most Y
for(int i = n - 2; i >= 0; i--)
{
// If i is the left
// most position of Y
if (k <= (n - i - 1))
{
// Put Y in their positions
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);
}
}
// Driver code
public static void Main(String[] args)
{
int n = 5, k = 7;
// Function call
kth_String(n, k);
}
}
// This code is contributed by Amit Katiyar
Javascript
输出:
YXXXY
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live