Find distinct characters in distinct substrings of a string
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++
#include <bits/stdc++.h>
using namespace std;
int countTotalDistinct(string str)
{
int cnt = 0;
set<string> items;
for ( int i = 0; i < str.length(); ++i) {
string temp = "" ;
set< char > ans;
for ( int j = i; j < str.length(); ++j) {
temp = temp + str[j];
ans.insert(str[j]);
if (items.find(temp) == items.end()) {
items.insert(temp);
cnt += ans.size();
}
}
}
return cnt;
}
int main()
{
string str = "ABCA" ;
cout << countTotalDistinct(str);
return 0;
}
|
Java
import java.util.HashSet;
class geeks
{
public static int countTotalDistinct(String str)
{
int cnt = 0 ;
HashSet<String> items = new HashSet<>();
for ( int i = 0 ; i < str.length(); ++i)
{
String temp = "" ;
HashSet<Character> ans = new HashSet<>();
for ( int j = i; j < str.length(); ++j)
{
temp = temp + str.charAt(j);
ans.add(str.charAt(j));
if (!items.contains(temp))
{
items.add(temp);
cnt += ans.size();
}
}
}
return cnt;
}
public static void main(String[] args)
{
String str = "ABCA" ;
System.out.println(countTotalDistinct(str));
}
}
|
Python3
def countTotalDistinct(string) :
cnt = 0 ;
items = set ();
for i in range ( len (string)) :
temp = "";
ans = set ();
for j in range (i, len (string)) :
temp = temp + string[j];
ans.add(string[j]);
if temp not in items :
items.add(temp);
cnt + = len (ans);
return cnt;
if __name__ = = "__main__" :
string = "ABCA" ;
print (countTotalDistinct(string));
|
C#
using System;
using System.Collections.Generic;
class geeks
{
public static int countTotalDistinct(String str)
{
int cnt = 0;
HashSet<String> items = new HashSet<String>();
for ( int i = 0; i < str.Length; ++i)
{
String temp = "" ;
HashSet< char > ans = new HashSet< char >();
for ( int j = i; j < str.Length; ++j)
{
temp = temp + str[j];
ans.Add(str[j]);
if (!items.Contains(temp))
{
items.Add(temp);
cnt += ans.Count;
}
}
}
return cnt;
}
public static void Main(String[] args)
{
String str = "ABCA" ;
Console.WriteLine(countTotalDistinct(str));
}
}
|
Javascript
<script>
function countTotalDistinct(str)
{
let cnt = 0;
let items = new Set();
for (let i = 0; i < str.length; ++i) {
let temp = "" ;
let ans = new Set();
for (let j = i; j < str.length; ++j) {
temp = temp + str[j];
ans.add(str[j]);
if (!items.has(temp)) {
items.add(temp);
cnt += ans.size;
}
}
}
return cnt;
}
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).
Last Updated :
07 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...