📌  相关文章
📜  具有非重复字符的所有唯一子字符串的计数

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

给定一个由小写字符组成的字符串str ,任务是找到具有非重复字符的唯一子字符串的总数。
例子:

方法:这个想法是迭代所有的子串。对于每个子字符串,检查每个特定字符以前是否出现过。如果是,则增加所需子串的数量。最后将此计数作为所有具有非重复字符的唯一子字符串的计数返回。
下面是上述方法的实现:

CPP
// C++ program to find the count of
// all unique sub-strings with
// non-repeating characters
 
#include 
using namespace std;
 
// Function to count all unique
// distinct character substrings
int distinctSubstring(string& P, int N)
{
    // Hashmap to store all substrings
    unordered_set S;
 
    // Iterate over all the substrings
    for (int i = 0; i < N; ++i) {
 
        // Boolean array to maintain all
        // characters encountered so far
        vector freq(26, false);
 
        // Variable to maintain the
        // substring till current position
        string s;
 
        for (int j = i; j < N; ++j) {
 
            // Get the position of the
            // character in the string
            int pos = P[j] - 'a';
 
            // Check if the character is
            // encountred
            if (freq[pos] == true)
                break;
 
            freq[pos] = true;
 
            // Add the current character
            // to the substring
            s += P[j];
 
            // Insert substring in Hashmap
            S.insert(s);
        }
    }
 
    return S.size();
}
 
// Driver code
int main()
{
    string S = "abba";
    int N = S.length();
 
    cout << distinctSubstring(S, N);
 
    return 0;
}


Java
// Java program to find the count of
// all unique sub-Strings with
// non-repeating characters
import java.util.*;
 
class GFG{
  
// Function to count all unique
// distinct character subStrings
static int distinctSubString(String P, int N)
{
    // Hashmap to store all subStrings
    HashSet S = new HashSet();
  
    // Iterate over all the subStrings
    for (int i = 0; i < N; ++i) {
  
        // Boolean array to maintain all
        // characters encountered so far
        boolean []freq = new boolean[26];
  
        // Variable to maintain the
        // subString till current position
        String s = "";
  
        for (int j = i; j < N; ++j) {
  
            // Get the position of the
            // character in the String
            int pos = P.charAt(j) - 'a';
  
            // Check if the character is
            // encountred
            if (freq[pos] == true)
                break;
  
            freq[pos] = true;
  
            // Add the current character
            // to the subString
            s += P.charAt(j);
  
            // Insert subString in Hashmap
            S.add(s);
        }
    }
  
    return S.size();
}
  
// Driver code
public static void main(String[] args)
{
    String S = "abba";
    int N = S.length();
  
    System.out.print(distinctSubString(S, N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find the count of
# all unique sub-strings with
# non-repeating characters
 
# Function to count all unique
# distinct character substrings
def distinctSubstring(P, N):
     
    # Hashmap to store all substrings
    S = dict()
 
    # Iterate over all the substrings
    for i in range(N):
 
        # Boolean array to maintain all
        # characters encountered so far
        freq = [False]*26
 
        # Variable to maintain the
        # subtill current position
        s = ""
 
        for j in range(i,N):
 
            # Get the position of the
            # character in the string
            pos = ord(P[j]) - ord('a')
 
            # Check if the character is
            # encountred
            if (freq[pos] == True):
                break
 
            freq[pos] = True
 
            # Add the current character
            # to the substring
            s += P[j]
 
            # Insert subin Hashmap
            S[s] = 1
 
    return len(S)
 
# Driver code
S = "abba"
N = len(S)
 
print(distinctSubstring(S, N))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the count of
// all unique sub-Strings with
// non-repeating characters
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to count all unique
// distinct character subStrings
static int distinctSubString(String P, int N)
{
    // Hashmap to store all subStrings
    HashSet S = new HashSet();
   
    // Iterate over all the subStrings
    for (int i = 0; i < N; ++i) {
   
        // Boolean array to maintain all
        // characters encountered so far
        bool []freq = new bool[26];
   
        // Variable to maintain the
        // subString till current position
        String s = "";
   
        for (int j = i; j < N; ++j) {
   
            // Get the position of the
            // character in the String
            int pos = P[j] - 'a';
   
            // Check if the character is
            // encountred
            if (freq[pos] == true)
                break;
   
            freq[pos] = true;
   
            // Add the current character
            // to the subString
            s += P[j];
   
            // Insert subString in Hashmap
            S.Add(s);
        }
    } 
    return S.Count;
}
   
// Driver code
public static void Main(String[] args)
{
    String S = "abba";
    int N = S.Length;
   
    Console.Write(distinctSubString(S, N));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:

4

时间复杂度: O(N 2 ) 其中 N 是字符串的长度。

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live