Find distinct characters in distinct substrings of a string
Last Updated :
07 Sep, 2021
Given a string str, the task is to find the count of distinct characters in all the distinct sub-strings of the given string.
Examples:
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
Approach: Take all possible sub-strings of the given string and use a set to check whether the current sub-string has been processed before. Now, for every distinct sub-string, count the distinct characters in it (again set can be used to do so). The sum of this count for all the distinct sub-strings is the final answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
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<string> 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<char> 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<String> 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<Character> 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<String> items = new HashSet<String>();
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<char> ans = new HashSet<char>();
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
<script>
// js implementation of the approach
// Function to return the count of distinct
// characters in all the distinct
// sub-strings of the given string
function countTotalDistinct(str)
{
let cnt = 0;
// To store all the sub-strings
let items = new Set();
for (let i = 0; i < str.length; ++i) {
// To store the current sub-string
let temp = "";
// To store the characters of the
// current sub-string
let ans = new Set();
for (let 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.has(temp)) {
// Insert it into the set
items.add(temp);
// Update the count of
// distinct characters
cnt += ans.size;
}
}
}
return cnt;
}
// Driver code
let str = "ABCA";
document.write(countTotalDistinct(str));
</script>
Time complexity: O(n^2)
As the nested loop is used the complexity is order if n^2
Space complexity: O(n)
two sets of size n are used so the complexity would be O(2n) nothing but O(n).
Similar Reads
Count substrings made up of a single distinct character Given a string S of length N, the task is to count the number of substrings made up of a single distinct character.Note: For the repetitive occurrences of the same substring, count all repetitions. Examples: Input: str = "geeksforgeeks"Output: 15Explanation: All substrings made up of a single distin
5 min read
Count of substrings having all distinct characters Given a string str consisting of lowercase alphabets, the task is to find the number of possible substrings (not necessarily distinct) that consists of distinct characters only.Examples: Input: Str = "gffg" Output: 6 Explanation: All possible substrings from the given string are, ( "g", "gf", "gff",
7 min read
Count number of substrings of a string consisting of same characters Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su
6 min read
Count the sum of count of distinct characters present in all Substrings Given a string S consisting of lowercase English letters of size N where (1 <= N <= 105), the task is to print the sum of the count of distinct characters N where (1 <= N <= 105)in all the substrings. Examples: Input: str = "abbca"Output: 28Explanation: The following are the substrings o
8 min read
Count number of substrings having at least K distinct characters Given a string S consisting of N characters and a positive integer K, the task is to count the number of substrings having at least K distinct characters.Examples:Input: S = "abcca", K = 3Output: 4Explanation:The substrings that contain at least K(= 3) distinct characters are:"abc": Count of distinc
7 min read