Count of all unique substrings with non-repeating characters
Last Updated :
24 Mar, 2023
Given a string str consisting of lowercase characters, the task is to find the total number of unique substrings with non-repeating characters.
Examples:
Input: str = “abba”
Output: 4
Explanation:
There are 4 unique substrings. They are: “a”, “ab”, “b”, “ba”.
Input: str = “acbacbacaa”
Output: 10
Approach: The idea is to iterate over all the substrings. For every substring, check whether each particular character has previously occurred or not. If so, then increase the count of required substrings. In the end return this count as count of all unique substrings with non-repeating characters.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int distinctSubstring(string& P, int N)
{
unordered_set<string> S;
for ( int i = 0; i < N; ++i) {
vector< bool > freq(26, false );
string s;
for ( int j = i; j < N; ++j) {
int pos = P[j] - 'a' ;
if (freq[pos] == true )
break ;
freq[pos] = true ;
s += P[j];
S.insert(s);
}
}
return S.size();
}
int main()
{
string S = "abba" ;
int N = S.length();
cout << distinctSubstring(S, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int distinctSubString(String P, int N)
{
HashSet<String> S = new HashSet<String>();
for ( int i = 0 ; i < N; ++i) {
boolean []freq = new boolean [ 26 ];
String s = "" ;
for ( int j = i; j < N; ++j) {
int pos = P.charAt(j) - 'a' ;
if (freq[pos] == true )
break ;
freq[pos] = true ;
s += P.charAt(j);
S.add(s);
}
}
return S.size();
}
public static void main(String[] args)
{
String S = "abba" ;
int N = S.length();
System.out.print(distinctSubString(S, N));
}
}
|
Python3
def distinctSubstring(P, N):
S = dict ()
for i in range (N):
freq = [ False ] * 26
s = ""
for j in range (i,N):
pos = ord (P[j]) - ord ( 'a' )
if (freq[pos] = = True ):
break
freq[pos] = True
s + = P[j]
S[s] = 1
return len (S)
S = "abba"
N = len (S)
print (distinctSubstring(S, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int distinctSubString(String P, int N)
{
HashSet<String> S = new HashSet<String>();
for ( int i = 0; i < N; ++i) {
bool []freq = new bool [26];
String s = "" ;
for ( int j = i; j < N; ++j) {
int pos = P[j] - 'a' ;
if (freq[pos] == true )
break ;
freq[pos] = true ;
s += P[j];
S.Add(s);
}
}
return S.Count;
}
public static void Main(String[] args)
{
String S = "abba" ;
int N = S.Length;
Console.Write(distinctSubString(S, N));
}
}
|
Javascript
<script>
function distinctSubstring(P, N)
{
var S = new Set();
for ( var i = 0; i < N; ++i) {
var freq = Array(26).fill( false );
var s = "" ;
for ( var j = i; j < N; ++j) {
var pos = P[j].charCodeAt(0) - 'a' .charCodeAt(0);
if (freq[pos] == true )
break ;
freq[pos] = true ;
s += P[j];
S.add(s);
}
}
return S.size;
}
var S = "abba" ;
var N = S.length;
document.write( distinctSubstring(S, N));
</script>
|
Time Complexity: O(N2) where N is the length of the string.
Auxiliary Space: O(N2), to store all substrings in hashmap.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...