📌  相关文章
📜  计算字符串中不同子串的数量

📅  最后修改于: 2021-10-27 08:27:05             🧑  作者: Mango

给定一个字符串,计算给定字符串的所有不同子字符串。
例子:

Input : abcd
Output : abcd abc ab a bcd bc b cd c d
All Elements are Distinct

Input : aaa
Output : aaa aa a aa a a
All elements are not Distinct

先决条件:打印给定数组的子数组
这个想法是使用哈希表( Java的HashSet)来存储所有生成的子字符串。最后我们返回 HashSet 的大小。

C++
// C++ program to count all distinct substrings in a string
#include
using namespace std;
 
int distinctSubstring(string str)
{
    // Put all distinct substring in a HashSet
    set result ;
 
    // List All Substrings
    for (int i = 0; i <= str.length(); i++)
    {
        for (int j = 1; j <= str.length()-i; j++)
        {
 
            // Add each substring in Set
            result.insert(str.substr(i, j));
        }
    }
 
    // Return size of the HashSet
    return result.size();
}
 
// Driver Code
int main()
{
    string str = "aaaa";
    cout << (distinctSubstring(str));
}
 
// This code is contributed by Rajput-Ji


Java
// Java program to count all distinct substrings in a string
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
public class DistinctSubstring {
 
    public static int distinctSubstring(String str)
    {
        // Put all distinct substring in a HashSet
        Set result = new HashSet();
 
        // List All Substrings
        for (int i = 0; i <= str.length(); i++) {
            for (int j = i + 1; j <= str.length(); j++) {
 
                // Add each substring in Set
                result.add(str.substring(i, j));
            }
        }
 
        // Return size of the HashSet
        return result.size();
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "aaaa";
        System.out.println(distinctSubstring(str));
    }
}


Python3
# Python3 program to count all distinct substrings in a string
 
def distinctSubstring(str):
    # Put all distinct substring in a HashSet
    result = set()
 
    # List All Substrings
    for i in range(len(str)+1):
        for j in range( i + 1, len(str)+1):
 
            # Add each substring in Set
            result.add(str[i:j]);
        # Return size of the HashSet
    return len(result);
 
# Driver Code
if __name__ == '__main__':
    str = "aaaa";
    print(distinctSubstring(str));
 
# This code has been contributed by 29AjayKumar


C#
// C# program to count all distinct
// substrings in a string
using System;
using System.Collections.Generic;
 
class DistinctSubstring
{
    public static int distinctSubstring(String str)
    {
        // Put all distinct substring in a HashSet
        HashSet result = new HashSet();
 
        // List All Substrings
        for (int i = 0; i <= str.Length; i++)
        {
            for (int j = i + 1; j <= str.Length; j++)
            {
 
                // Add each substring in Set
                result.Add(str.Substring(i, j - i));
            }
        }
 
        // Return size of the HashSet
        return result.Count;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "aaaa";
        Console.WriteLine(distinctSubstring(str));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


C++
// C++ program to count all distinct
// substrings in a string
#include 
using namespace std;
 
set distinctSubstring(string str)
{
 
    // Put all distinct substrings
    // in the Hashset
    set result;
 
    // List all substrings
    for(int i = 0; i <= str.length(); i++)
    {
        for(int j = i + 1; j <= str.length(); j++)
        {
 
            // Add each substring in Set
            result.insert(str.substr(i, j));
        }
    }
 
    // Return the hashset
    return result;
}
 
// Driver code
int main()
{
    string str = "aaaa";
    set subs = distinctSubstring(str);
 
    cout << "Distinct Substrings are: \n";
    for(auto i : subs)
        cout << i << endl;
}
 
// This code is contributed by Ronak Mangal


Java
// Java program to count all distinct substrings in a string
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
public class DistinctSubstring {
 
    public static Set distinctSubstring(String str)
    {
 
        // Put all distinct substring in a HashSet
        Set result = new HashSet();
 
        // List All Substrings
        for (int i = 0; i <= str.length(); i++) {
            for (int j = i + 1; j <= str.length(); j++) {
 
                // Add each substring in Set
                result.add(str.substring(i, j));
            }
        }
 
        // Return the HashSet
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "aaaa";
        Set subs = distinctSubstring(str);
 
        System.out.println("Distinct Substrings are: ");
        for (String s : subs) {
            System.out.println(s);
        }
    }
}


Python3
# Python3 program to count all distinct
# substrings in a string
 
def distinctSubstring(str):
 
    # Put all distinct substring in a HashSet
    result = set();
 
    # List All Substrings
    for i in range(len(str)):
        for j in range(i + 1, len(str) + 1):
 
            # Add each substring in Set
            result.add(str[i:j]);
 
        # Return the HashSet
    return result;
 
# Driver Code
if __name__ == '__main__':
 
    str = "aaaa";
    subs = distinctSubstring(str);
 
    print("Distinct Substrings are: ");
    for s in subs:
        print(s);
 
# This code is contributed by 29AjayKumar


C#
// C# program to count all distinct
// substrings in a string
using System;
using System.Collections.Generic;
 
class GFG
{
    public static HashSet distinctSubstring(String str)
    {
 
        // Put all distinct substring in a HashSet
        HashSet result = new HashSet();
 
        // List All Substrings
        for (int i = 0; i <= str.Length; i++)
        {
            for (int j = i + 1; j <= str.Length; j++)
            {
 
                // Add each substring in Set
                result.Add(str.Substring(i, j - i));
            }
        }
 
        // Return the HashSet
        return result;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "aaaa";
        HashSet subs = distinctSubstring(str);
 
        Console.WriteLine("Distinct Substrings are: ");
        foreach (String s in subs)
        {
            Console.WriteLine(s);
        }
    }
}
 
// This code is contributed by 29AjayKumar


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// valid sub-strings
void printSubstrings(string s)
{
 
    // To store distinct output substrings
    unordered_set us;
 
    // Traverse through the given string and
    // one by one generate substrings beginning
    // from s[i].
    for (int i = 0; i < s.size(); ++i) {
 
        // One by one generate substrings ending
        // with s[j]
        string ss = "";
        for (int j = i; j < s.size(); ++j) {
 
            ss = ss + s[j];
            us.insert(ss);
        }
    }
 
    // Print all substrings one by one
    for (auto s : us)
        cout << s << " ";
}
 
// Driver code
int main()
{
    string str = "aaabc";
    printSubstrings(str);
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of
// valid sub-Strings
static void printSubStrings(String s)
{
 
    // To store distinct output subStrings
    HashSet us = new HashSet();
 
    // Traverse through the given String and
    // one by one generate subStrings beginning
    // from s[i].
    for (int i = 0; i < s.length(); ++i)
    {
 
        // One by one generate subStrings ending
        // with s[j]
        String ss = "";
        for (int j = i; j < s.length(); ++j)
        {
            ss = ss + s.charAt(j);
            us.add(ss);
        }
    }
 
    // Print all subStrings one by one
    for (String str : us)
        System.out.print(str + " ");
}
 
// Driver code
public static void main(String[] args)
{
    String str = "aaabc";
    printSubStrings(str);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the count of
# valid sub-Strings
def printSubStrings(s):
 
    # To store distinct output subStrings
    us = set();
 
    # Traverse through the given String and
    # one by one generate subStrings beginning
    # from s[i].
    for i in range(len(s)):
 
        # One by one generate subStrings ending
        # with s[j]
        ss = "";
        for j in range(i, len(s)):
            ss = ss + s[j];
            us.add(ss);
         
    # Prall subStrings one by one
    for str in us:
        print(str, end=" ");
 
# Driver code
if __name__ == '__main__':
    str = "aaabc";
    printSubStrings(str);
     
# This code is contributed by 29AjayKumar


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to return the count of
// valid sub-Strings
static void printSubStrings(String s)
{
 
    // To store distinct output subStrings
    HashSet us = new HashSet();
 
    // Traverse through the given String and
    // one by one generate subStrings
    // beginning from s[i].
    for (int i = 0; i < s.Length; ++i)
    {
 
        // One by one generate subStrings
        // ending with s[j]
        String ss = "";
        for (int j = i; j < s.Length; ++j)
        {
            ss = ss + s[j];
            us.Add(ss);
        }
    }
 
    // Print all subStrings one by one
    foreach (String str in us)
        Console.Write(str + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "aaabc";
    printSubStrings(str);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
4

如何打印不同的子字符串?

C++

// C++ program to count all distinct
// substrings in a string
#include 
using namespace std;
 
set distinctSubstring(string str)
{
 
    // Put all distinct substrings
    // in the Hashset
    set result;
 
    // List all substrings
    for(int i = 0; i <= str.length(); i++)
    {
        for(int j = i + 1; j <= str.length(); j++)
        {
 
            // Add each substring in Set
            result.insert(str.substr(i, j));
        }
    }
 
    // Return the hashset
    return result;
}
 
// Driver code
int main()
{
    string str = "aaaa";
    set subs = distinctSubstring(str);
 
    cout << "Distinct Substrings are: \n";
    for(auto i : subs)
        cout << i << endl;
}
 
// This code is contributed by Ronak Mangal

Java

// Java program to count all distinct substrings in a string
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
 
public class DistinctSubstring {
 
    public static Set distinctSubstring(String str)
    {
 
        // Put all distinct substring in a HashSet
        Set result = new HashSet();
 
        // List All Substrings
        for (int i = 0; i <= str.length(); i++) {
            for (int j = i + 1; j <= str.length(); j++) {
 
                // Add each substring in Set
                result.add(str.substring(i, j));
            }
        }
 
        // Return the HashSet
        return result;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String str = "aaaa";
        Set subs = distinctSubstring(str);
 
        System.out.println("Distinct Substrings are: ");
        for (String s : subs) {
            System.out.println(s);
        }
    }
}

蟒蛇3

# Python3 program to count all distinct
# substrings in a string
 
def distinctSubstring(str):
 
    # Put all distinct substring in a HashSet
    result = set();
 
    # List All Substrings
    for i in range(len(str)):
        for j in range(i + 1, len(str) + 1):
 
            # Add each substring in Set
            result.add(str[i:j]);
 
        # Return the HashSet
    return result;
 
# Driver Code
if __name__ == '__main__':
 
    str = "aaaa";
    subs = distinctSubstring(str);
 
    print("Distinct Substrings are: ");
    for s in subs:
        print(s);
 
# This code is contributed by 29AjayKumar

C#

// C# program to count all distinct
// substrings in a string
using System;
using System.Collections.Generic;
 
class GFG
{
    public static HashSet distinctSubstring(String str)
    {
 
        // Put all distinct substring in a HashSet
        HashSet result = new HashSet();
 
        // List All Substrings
        for (int i = 0; i <= str.Length; i++)
        {
            for (int j = i + 1; j <= str.Length; j++)
            {
 
                // Add each substring in Set
                result.Add(str.Substring(i, j - i));
            }
        }
 
        // Return the HashSet
        return result;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        String str = "aaaa";
        HashSet subs = distinctSubstring(str);
 
        Console.WriteLine("Distinct Substrings are: ");
        foreach (String s in subs)
        {
            Console.WriteLine(s);
        }
    }
}
 
// This code is contributed by 29AjayKumar
输出:
Distinct Substrings are: 
aa
aaa
a
aaaa

优化:
我们可以进一步优化上面的代码。 substr()函数在线性时间内工作。我们可以使用将当前字符附加到前一个子字符串来获取当前子字符串。

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// valid sub-strings
void printSubstrings(string s)
{
 
    // To store distinct output substrings
    unordered_set us;
 
    // Traverse through the given string and
    // one by one generate substrings beginning
    // from s[i].
    for (int i = 0; i < s.size(); ++i) {
 
        // One by one generate substrings ending
        // with s[j]
        string ss = "";
        for (int j = i; j < s.size(); ++j) {
 
            ss = ss + s[j];
            us.insert(ss);
        }
    }
 
    // Print all substrings one by one
    for (auto s : us)
        cout << s << " ";
}
 
// Driver code
int main()
{
    string str = "aaabc";
    printSubstrings(str);
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of
// valid sub-Strings
static void printSubStrings(String s)
{
 
    // To store distinct output subStrings
    HashSet us = new HashSet();
 
    // Traverse through the given String and
    // one by one generate subStrings beginning
    // from s[i].
    for (int i = 0; i < s.length(); ++i)
    {
 
        // One by one generate subStrings ending
        // with s[j]
        String ss = "";
        for (int j = i; j < s.length(); ++j)
        {
            ss = ss + s.charAt(j);
            us.add(ss);
        }
    }
 
    // Print all subStrings one by one
    for (String str : us)
        System.out.print(str + " ");
}
 
// Driver code
public static void main(String[] args)
{
    String str = "aaabc";
    printSubStrings(str);
}
}
 
// This code is contributed by Rajput-Ji

蟒蛇3

# Python3 implementation of the approach
 
# Function to return the count of
# valid sub-Strings
def printSubStrings(s):
 
    # To store distinct output subStrings
    us = set();
 
    # Traverse through the given String and
    # one by one generate subStrings beginning
    # from s[i].
    for i in range(len(s)):
 
        # One by one generate subStrings ending
        # with s[j]
        ss = "";
        for j in range(i, len(s)):
            ss = ss + s[j];
            us.add(ss);
         
    # Prall subStrings one by one
    for str in us:
        print(str, end=" ");
 
# Driver code
if __name__ == '__main__':
    str = "aaabc";
    printSubStrings(str);
     
# This code is contributed by 29AjayKumar

C#

// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to return the count of
// valid sub-Strings
static void printSubStrings(String s)
{
 
    // To store distinct output subStrings
    HashSet us = new HashSet();
 
    // Traverse through the given String and
    // one by one generate subStrings
    // beginning from s[i].
    for (int i = 0; i < s.Length; ++i)
    {
 
        // One by one generate subStrings
        // ending with s[j]
        String ss = "";
        for (int j = i; j < s.Length; ++j)
        {
            ss = ss + s[j];
            us.Add(ss);
        }
    }
 
    // Print all subStrings one by one
    foreach (String str in us)
        Console.Write(str + " ");
}
 
// Driver code
public static void Main(String[] args)
{
    String str = "aaabc";
    printSubStrings(str);
}
}
 
// This code is contributed by Rajput-Ji

Javascript


输出:
bc b abc ab aabc aa aaa c a aaab aab aaabc