📌  相关文章
📜  给定字符串的第K个按字典顺序最小的唯一子字符串

📅  最后修改于: 2021-04-22 01:02:32             🧑  作者: Mango

给定字符串S。任务是按字典顺序打印第K个s的不同子字符串中最小的一个。
s的子字符串是通过去除s中的非空连续部分而获得的字符串。
例如,如果s = ababc,则a,bab和ababc是s的子字符串,而ac,z和空字符串则不是。另外,我们说子字符串与字符串不同时也不同。

例子:

方法:对于任意字符串t,其每个适当的后缀在字典上小于t,并且t的字典等级至少为| t |。因此,答案的长度最多为K。
生成s的所有子串,它们的长度最大为K。对它们进行排序,唯一化并打印第K个子串,其中N = | S |。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include
using namespace std;
 
void kThLexString(string st, int k, int n)
{
     
    // Set to store the unique substring
    set z;
     
    for(int i = 0; i < n; i++)
    {
         
       // String to create each substring
       string pp;
        
       for(int j = i; j < i + k; j++)
       {
          if (j >= n)
              break;
          pp += st[j];
           
          // Adding to set
          z.insert(pp);
       }
    }
     
    // Converting set into a list
    vector fin(z.begin(), z.end());
     
    // Sorting the strings int the list
    // into lexicographical order
    sort(fin.begin(), fin.end());
 
    // Printing kth substring
    cout << fin[k - 1];
}
 
// Driver code
int main()
{
    string s = "geeksforgeeks";
    int k = 5;
    int n = s.length();
     
    kThLexString(s, k, n);
}
 
// This code is contributed by yatinagg


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
     
public static void kThLexString(String st,
                                int k, int n)
{
    // Set to store the unique substring
    Set z = new HashSet();
     
    for(int i = 0; i < n; i++)
    {
        // String to create each substring
        String pp = "";
     
        for(int j = i; j < i + k; j++)
        {
        if (j >= n)
            break;
        pp += st.charAt(j);
     
        // Adding to set
        z.add(pp);
        }
    }
     
    // Converting set into a list
    Vector fin = new Vector();
    fin.addAll(z);
     
    // Sorting the strings int the list
    // into lexicographical order
    Collections.sort(fin);
     
    // Printing kth substring
    System.out.print(fin.get(k - 1));
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "geeksforgeeks";
    int k = 5;
    int n = s.length();
    kThLexString(s, k, n);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation of the above approach
def kThLexString(st, k, n):
    # Set to store the unique substring
    z = set()
 
    for i in range(n):
        # String to create each substring
        pp = ""
 
        for j in range(i, i + k):
 
            if (j >= n):
 
                break
 
            pp += s[j]
            # adding to set
            z.add(pp)
 
    # converting set into a list
    fin = list(z)
     
    # sorting the strings int the list
    # into lexicographical order
    fin.sort()
 
    # printing kth substring
    print(fin[k - 1])
 
 
s = "geeksforgeeks"
 
k = 5
 
n = len(s)
kThLexString(s, k, n)


C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
using System.Collections;
 
class GFG{
      
public static void kThLexString(string st,
                                int k, int n)
{
     
    // Set to store the unique substring
    HashSet z = new HashSet();
      
    for(int i = 0; i < n; i++)
    {
         
        // String to create each substring
        string pp = "";
      
        for(int j = i; j < i + k; j++)
        {
            if (j >= n)
                break;
                 
            pp += st[j];
          
            // Adding to set
            z.Add(pp);
        }
    }
     
    // Converting set into a list
    ArrayList fin = new ArrayList();
     
    foreach(string s in z)
    {
        fin.Add(s);
    }
     
    // Sorting the strings int the list
    // into lexicographical order
    fin.Sort();
      
    // Printing kth substring
    Console.Write(fin[k - 1]);
}
  
// Driver Code
public static void Main(string[] args)
{
    string s = "geeksforgeeks";
    int k = 5;
    int n = s.Length;
     
    kThLexString(s, k, n);
}
}
 
// This code is contributed by rutvik_56


输出:
eeksf