📌  相关文章
📜  按字典顺序查找由 n-2 个 X 和 2 个 Y 组成的第 k 个字符串

📅  最后修改于: 2021-09-02 07:39:47             🧑  作者: Mango

给定两个数字NK ,如果起始字符串包含(N-2) 个 x2 个 Y ,则任务是按字典顺序找到第K字符串
笔记:

例子:

方法
为了找到第k位置的字符串,我们必须遵循以下步骤——

  1. 我们必须通过迭代从n-20 的所有位置来找到最左边出现的‘Y’的位置。
  2. 现在在迭代时,如果k<=ni-1那么这是‘Y’最左边出现的所需位置,最右边出现的位置是nk,所以我们可以打印答案。
  3. 否则,让我们将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