Open In App

Contiguous unique substrings with the given length L

Last Updated : 02 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string str and an integer L. The task is to print all the unique substring of length L from string str
Examples: 

Input: str = “abca”, L=3 
Output: “abc”, “bca” 
Input: str = “aaaa”, L=3 
Output: “aaa”

Approach: Firstly generate all the substring of length L and then by using set we can insert unique sub-string till the length L and then print the result. Below is the implementation of the above approach: 

C++




// C++ implementation
#include <bits/stdc++.h>
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<string> 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<String> st = new HashSet<String>();
 
    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<String> st = new HashSet<String>();
 
    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


Javascript




// Javascript ptogram for the above approach
 
// Function to print the unique sub-string of length n
function result(s, n) {
  // Create a new Set to store the unique sub-strings
  let st = new Set();
 
  // Loop through each character in the string s
  for (let i = 0; i < s.length; i++) {
    // Initialize an empty string
    let ans = "";
 
    // Loop through each character starting from index i
    for (let j = i; j < s.length; j++) {
      // Add the current character to the string ans
      ans += s[j];
 
      // If the length of the string ans is equal to n,
      // then add it to the set and break out of the loop
      if (ans.length === n) {
        st.add(ans);
        break;
      }
    }
  }
 
  // Print the set of unique sub-strings
  for (let it of st) {
    process.stdout.write(it + " ");
  }
}
 
// Driver code
let s = "abca";
let n = 3;
result(s, n);
 
// This code is contributed by codebraxnzt


Output:

bca abc


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads