在字符串的不同子字符串中查找不同的字符
给定一个字符串str ,任务是找出给定字符串的所有不同子字符串中不同字符的计数。
例子:
Input: str = “ABCA”
Output: 18
Distinct sub-strings | Distinct characters |
---|---|
A | 1 |
AB | 2 |
ABC | 3 |
ABCA | 3 |
B | 1 |
BC | 2 |
BCA | 3 |
C | 1 |
CA | 2 |
Hence, 1 + 2 + 3 + 3 + 1 + 2 + 3 + 1 + 2 = 18
Input: str = “AAAB”
Output: 10
方法:取给定字符串的所有可能的子字符串,并使用一个集合来检查当前子字符串之前是否已经被处理过。现在,对于每个不同的子字符串,计算其中的不同字符(同样可以使用 set 来计算)。所有不同子字符串的计数总和是最终答案。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the count of distinct
// characters in all the distinct
// sub-strings of the given string
int countTotalDistinct(string str)
{
int cnt = 0;
// To store all the sub-strings
set items;
for (int i = 0; i < str.length(); ++i) {
// To store the current sub-string
string temp = "";
// To store the characters of the
// current sub-string
set ans;
for (int j = i; j < str.length(); ++j) {
temp = temp + str[j];
ans.insert(str[j]);
// If current sub-string hasn't
// been stored before
if (items.find(temp) == items.end()) {
// Insert it into the set
items.insert(temp);
// Update the count of
// distinct characters
cnt += ans.size();
}
}
}
return cnt;
}
// Driver code
int main()
{
string str = "ABCA";
cout << countTotalDistinct(str);
return 0;
}
Java
// Java implementation of the approach
import java.util.HashSet;
class geeks
{
// Function to return the count of distinct
// characters in all the distinct
// sub-strings of the given string
public static int countTotalDistinct(String str)
{
int cnt = 0;
// To store all the sub-strings
HashSet items = new HashSet<>();
for (int i = 0; i < str.length(); ++i)
{
// To store the current sub-string
String temp = "";
// To store the characters of the
// current sub-string
HashSet ans = new HashSet<>();
for (int j = i; j < str.length(); ++j)
{
temp = temp + str.charAt(j);
ans.add(str.charAt(j));
// If current sub-string hasn't
// been stored before
if (!items.contains(temp))
{
// Insert it into the set
items.add(temp);
// Update the count of
// distinct characters
cnt += ans.size();
}
}
}
return cnt;
}
// Driver code
public static void main(String[] args)
{
String str = "ABCA";
System.out.println(countTotalDistinct(str));
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python3 implementation of the approach
# Function to return the count of distinct
# characters in all the distinct
# sub-strings of the given string
def countTotalDistinct(string) :
cnt = 0;
# To store all the sub-strings
items = set();
for i in range(len(string)) :
# To store the current sub-string
temp = "";
# To store the characters of the
# current sub-string
ans = set();
for j in range(i, len(string)) :
temp = temp + string[j];
ans.add(string[j]);
# If current sub-string hasn't
# been stored before
if temp not in items :
# Insert it into the set
items.add(temp);
# Update the count of
# distinct characters
cnt += len(ans);
return cnt;
# Driver code
if __name__ == "__main__" :
string = "ABCA";
print(countTotalDistinct(string));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
class geeks
{
// Function to return the count of distinct
// characters in all the distinct
// sub-strings of the given string
public static int countTotalDistinct(String str)
{
int cnt = 0;
// To store all the sub-strings
HashSet items = new HashSet();
for (int i = 0; i < str.Length; ++i)
{
// To store the current sub-string
String temp = "";
// To store the characters of the
// current sub-string
HashSet ans = new HashSet();
for (int j = i; j < str.Length; ++j)
{
temp = temp + str[j];
ans.Add(str[j]);
// If current sub-string hasn't
// been stored before
if (!items.Contains(temp))
{
// Insert it into the set
items.Add(temp);
// Update the count of
// distinct characters
cnt += ans.Count;
}
}
}
return cnt;
}
// Driver code
public static void Main(String[] args)
{
String str = "ABCA";
Console.WriteLine(countTotalDistinct(str));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
18
时间复杂度: O(n^2)
由于使用了嵌套循环,因此如果 n^2 复杂度是有序的
空间复杂度: O(n)
使用了两组大小 n,因此复杂度将是 O(2n),只不过是 O(n)。