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

📅  最后修改于: 2021-06-26 22:42:09             🧑  作者: Mango

给定两个数字NK ,如果起始字符串包含(N-2)x的第一个字母,然后包含2个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现场课程》和《 Geeks现场课程美国》。