📜  给定长度的第K个字典字符串

📅  最后修改于: 2021-05-06 20:33:18             🧑  作者: Mango

给定两个整数NK ,任务是从字典上找到长度为N的K字符串。如果长度为N的可能字符串的数量小于K ,则打印-1

例子:

方法:

  • 让我们假设一个长度为N的字符串为以26为底的整数
  • 例如,如果N = 3,则第一个字符串将为s =“ aaa”,其整数的基数26表示将为0 0 0 ,第二个字符串将为s =“ aab”,且基数26的表示形式将为0 0 1和很快。因此,每个数字的值都可以在[0,25]之内
  • 从最后一位开始,我们需要将其更改为所有可能的值,然后是倒数第二位,依此类推。
  • 因此,简化的问题是将十进制数K的表示形式转换为26个基数。您可以阅读这篇文章,以清楚地知道如何执行此操作。
  • 找到以26为底的整数形式的答案后,我们将把每个数字转换成它的等效字符,其中0‘a’1‘b’ ,…。 24‘y’25‘z’

    下面是上述方法的实现:

    C++
    // C++ program to find the K-th 
    // lexicographical string of length N
    #include 
    using namespace std;
      
    string find_kth_String_of_n(int n, int k) 
    {
          
        // Initialize the array to store
        // the base 26 representation of  
        // K with all zeroes, that is,  
        // the initial String consists
        // of N a's
        int d[n] = {0};
      
        // Start filling all the
        // N slots for the base 26
        // representation of K
        for(int i = n - 1; i > -1; i--) 
        {
              
           // Store the remainder
           d[i] = k % 26;
             
           // Reduce K
           k /= 26;
        }
          
        // If K is greater than the
        // possible number of strings
        // that can be represented by
        // a string of length N
        if (k > 0)
            return "-1";
      
        string s = "";
          
        // Store the Kth lexicographical
        // string
        for(int i = 0; i < n; i++)
           s += (d[i] + ('a'));
      
        return s;
    }
      
    // Driver Code
    int main()
    {
        int n = 3;
        int k = 10;
          
        // Reducing k value by 1 because
        // our stored value starts from 0
        k -= 1;
          
        cout << find_kth_String_of_n(n, k);
        return 0;
    }
      
    // This code is contributed by 29AjayKumar


    Java
    // Java program to find the
    // K-th lexicographical String
    // of length N
    class GFG{
          
    static String find_kth_String_of_n(int n, int k) 
    {
        // Initialize the array to
        // store the base 26
        // representation of K
        // with all zeroes, that is,
        // the initial String consists
        // of N a's
        int[] d = new int[n];
      
        // Start filling all the
        // N slots for the base 26
        // representation of K
        for (int i = n - 1; i > -1; i--) 
        {
      
            // Store the remainder
            d[i] = k % 26;
      
            // Reduce K
            k /= 26;
        }
          
        // If K is greater than the
        // possible number of Strings
        // that can be represented by
        // a String of length N
        if (k > 0)
            return "-1";
      
        String s = "";
        // Store the Kth lexicographical
        // String
        for (int i = 0; i < n; i++)
            s += (char)(d[i] + ('a'));
      
        return s;
    }
      
    public static void main(String[] args) 
    {
        int n = 3;
        int k = 10;
          
        // Reducing k value by 1 because
        // our stored value starts from 0
        k -= 1;
        System.out.println(find_kth_String_of_n(n, k));
    }
    }
      
    // This code is contributed by PrinciRaj1992


    Python3
    # Python program to find the
    # K-th lexicographical string
    # of length N
    def find_kth_string_of_n(n, k):
        # Initialize the array to
        # store the base 26 
        # representation of K
        # with all zeroes, that is,
        # the initial string consists 
        # of N a's
        d =[0 for i in range(n)]
      
        # Start filling all the 
        # N slots for the base 26
        # representation of K
        for i in range(n - 1, -1, -1):
      
            # Store the remainder
            d[i]= k % 26
      
            # Reduce K
            k//= 26
      
        # If K is greater than the
        # possible number of strings
        # that can be represented by 
        # a string of length N
        if k>0:
            return -1
          
          
        s =""
        # Store the Kth lexicographical 
        # string
        for i in range(n):
            s+= chr(d[i]+ord('a'))
          
        return s     
    n = 3
    k = 10
    # Reducing k value by 1 because 
    # our stored value starts from 0
    k-= 1 
    print(find_kth_string_of_n(n, k))


    C#
    // C# program to find the
    // K-th lexicographical String
    // of length N
    using System;
    class GFG{
          
    static String find_kth_String_of_n(int n, int k) 
    {
        // Initialize the array to
        // store the base 26
        // representation of K
        // with all zeroes, that is,
        // the initial String consists
        // of N a's
        int[] d = new int[n];
      
        // Start filling all the
        // N slots for the base 26
        // representation of K
        for (int i = n - 1; i > -1; i--) 
        {
      
            // Store the remainder
            d[i] = k % 26;
      
            // Reduce K
            k /= 26;
        }
          
        // If K is greater than the
        // possible number of Strings
        // that can be represented by
        // a String of length N
        if (k > 0)
            return "-1";
      
        string s = "";
          
        // Store the Kth lexicographical
        // String
        for (int i = 0; i < n; i++)
            s += (char)(d[i] + ('a'));
      
        return s;
    }
      
    // Driver Code
    public static void Main() 
    {
        int n = 3;
        int k = 10;
          
        // Reducing k value by 1 because
        // our stored value starts from 0
        k -= 1;
        Console.Write(find_kth_String_of_n(n, k));
    }
    }
      
    // This code is contributed by Code_Mech


    输出:
    aaj
    

    时间复杂度: O(N)