📌  相关文章
📜  具有给定长度 L 的连续唯一子串

📅  最后修改于: 2022-05-13 01:57:07.407000             🧑  作者: Mango

具有给定长度 L 的连续唯一子串

给定一个字符串str和一个整数L 。任务是打印字符串str中所有长度为L的唯一子字符串。

例子:

方法:
首先生成所有长度为L的子字符串,然后使用 set 我们可以插入唯一的子字符串直到长度为 L,然后打印结果。

下面是上述方法的实现:

C++
// C++ implementation
#include 
using namespace std;
  
// Function to print the
// unique sub-string of length n
void result(string s, int n)
{
    // set to store the strings
    unordered_set st;
  
    for (int i = 0; i < (int)s.size(); i++) {
  
        string ans = "";
        for (int j = i; j < (int)s.size(); j++) {
  
            ans += s[j];
  
            // if the size of the string
            // is equal to 1 then insert
            if (ans.size() == n) {
  
                // inserting unique
                // sub-string of length L
                st.insert(ans);
                break;
            }
        }
    }
  
    // Printing the set of strings
    for (auto it : st)
        cout << it << " ";
}
  
// Driver Code
int main()
{
    string s = "abca";
    int n = 3;
  
    // Function calling
    result(s, n);
    return 0;
}


Java
// Java implementation of above approach
import java.util.*;
class GFG 
{
  
// Function to print the
// unique sub-String of length n
static void result(String s, int n)
{
    // set to store the Strings
    HashSet st = new HashSet();
  
    for (int i = 0; i < (int)s.length(); i++) 
    {
        String ans = "";
        for (int j = i; j < (int)s.length(); j++)
        {
            ans += s.charAt(j);
  
            // if the size of the String
            // is equal to 1 then insert
            if (ans.length()== n)
            {
  
                // inserting unique
                // sub-String of length L
                st.add(ans);
                break;
            }
        }
    }
  
    // Printing the set of Strings
    for (String it : st)
        System.out.print(it + " ");
}
  
// Driver Code
public static void main(String[] args)
{
    String s = "abca";
    int n = 3;
  
    // Function calling
    result(s, n);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the above approach 
  
# Function to print the 
# unique sub-string of length n 
def result(s, n) : 
  
    # set to store the strings 
    st = set(); 
  
    for i in range(len(s)) : 
  
        ans = ""; 
        for j in range(i, len(s)) :
  
            ans += s[j]; 
  
            # if the size of the string 
            # is equal to 1 then insert 
            if (len(ans) == n) :
  
                # inserting unique 
                # sub-string of length L 
                st.add(ans); 
                break; 
  
    # Printing the set of strings 
    for it in st :
        print(it, end = " "); 
  
# Driver Code 
if __name__ == "__main__" : 
  
    s = "abca"; 
    n = 3; 
  
    # Function calling 
    result(s, n); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation for above approach
using System;
using System.Collections.Generic; 
      
class GFG 
{
  
// Function to print the
// unique sub-String of length n
static void result(String s, int n)
{
    // set to store the Strings
    HashSet st = new HashSet();
  
    for (int i = 0; i < s.Length; i++) 
    {
        String ans = "";
        for (int j = i; j < s.Length; j++)
        {
            ans += s[j];
  
            // if the size of the String
            // is equal to 1 then insert
            if (ans.Length == n)
            {
  
                // inserting unique
                // sub-String of length L
                st.Add(ans);
                break;
            }
        }
    }
  
    // Printing the set of Strings
    foreach (String it in st)
        Console.Write(it + " ");
}
  
// Driver Code
public static void Main(String[] args)
{
    String s = "abca";
    int n = 3;
  
    // Function calling
    result(s, n);
}
}
  
// This code is contributed by 29AjayKumar


输出:
bca abc