Count of all unique substrings with non-repeating characters Last Updated : 24 Mar, 2023 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice 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 // C++ program to find the count of // all unique sub-strings with // non-repeating characters #include <bits/stdc++.h> using namespace std; // Function to count all unique // distinct character substrings int distinctSubstring(string& P, int N) { // Hashmap to store all substrings unordered_set<string> S; // Iterate over all the substrings for (int i = 0; i < N; ++i) { // Boolean array to maintain all // characters encountered so far vector<bool> freq(26, false); // Variable to maintain the // substring till current position string s; for (int j = i; j < N; ++j) { // Get the position of the // character in the string int pos = P[j] - 'a'; // Check if the character is // encountred if (freq[pos] == true) break; freq[pos] = true; // Add the current character // to the substring s += P[j]; // Insert substring in Hashmap S.insert(s); } } return S.size(); } // Driver code int main() { string S = "abba"; int N = S.length(); cout << distinctSubstring(S, N); return 0; } Java // Java program to find the count of // all unique sub-Strings with // non-repeating characters import java.util.*; class GFG{ // Function to count all unique // distinct character subStrings static int distinctSubString(String P, int N) { // Hashmap to store all subStrings HashSet<String> S = new HashSet<String>(); // Iterate over all the subStrings for (int i = 0; i < N; ++i) { // Boolean array to maintain all // characters encountered so far boolean []freq = new boolean[26]; // Variable to maintain the // subString till current position String s = ""; for (int j = i; j < N; ++j) { // Get the position of the // character in the String int pos = P.charAt(j) - 'a'; // Check if the character is // encountred if (freq[pos] == true) break; freq[pos] = true; // Add the current character // to the subString s += P.charAt(j); // Insert subString in Hashmap S.add(s); } } return S.size(); } // Driver code public static void main(String[] args) { String S = "abba"; int N = S.length(); System.out.print(distinctSubString(S, N)); } } // This code is contributed by Rajput-Ji Python3 # Python3 program to find the count of # all unique sub-strings with # non-repeating characters # Function to count all unique # distinct character substrings def distinctSubstring(P, N): # Hashmap to store all substrings S = dict() # Iterate over all the substrings for i in range(N): # Boolean array to maintain all # characters encountered so far freq = [False]*26 # Variable to maintain the # substring till current position s = "" for j in range(i,N): # Get the position of the # character in the string pos = ord(P[j]) - ord('a') # Check if the character is # encountred if (freq[pos] == True): break freq[pos] = True # Add the current character # to the substring s += P[j] # Insert substring in Hashmap S[s] = 1 return len(S) # Driver code S = "abba" N = len(S) print(distinctSubstring(S, N)) # This code is contributed by mohit kumar 29 C# // C# program to find the count of // all unique sub-Strings with // non-repeating characters using System; using System.Collections.Generic; class GFG{ // Function to count all unique // distinct character subStrings static int distinctSubString(String P, int N) { // Hashmap to store all subStrings HashSet<String> S = new HashSet<String>(); // Iterate over all the subStrings for (int i = 0; i < N; ++i) { // Boolean array to maintain all // characters encountered so far bool []freq = new bool[26]; // Variable to maintain the // subString till current position String s = ""; for (int j = i; j < N; ++j) { // Get the position of the // character in the String int pos = P[j] - 'a'; // Check if the character is // encountred if (freq[pos] == true) break; freq[pos] = true; // Add the current character // to the subString s += P[j]; // Insert subString in Hashmap S.Add(s); } } return S.Count; } // Driver code public static void Main(String[] args) { String S = "abba"; int N = S.Length; Console.Write(distinctSubString(S, N)); } } // This code is contributed by Rajput-Ji JavaScript <script> // JavaScript program to find the count of // all unique sub-strings with // non-repeating characters // Function to count all unique // distinct character substrings function distinctSubstring(P, N) { // Hashmap to store all substrings var S = new Set(); // Iterate over all the substrings for (var i = 0; i < N; ++i) { // Boolean array to maintain all // characters encountered so far var freq = Array(26).fill(false); // Variable to maintain the // substring till current position var s = ""; for (var j = i; j < N; ++j) { // Get the position of the // character in the string var pos = P[j].charCodeAt(0) - 'a'.charCodeAt(0); // Check if the character is // encountred if (freq[pos] == true) break; freq[pos] = true; // Add the current character // to the substring s += P[j]; // Insert substring in Hashmap S.add(s); } } return S.size; } // Driver code var S = "abba"; var N = S.length; document.write( distinctSubstring(S, N)); </script> Output: 4 Time Complexity: O(N2) where N is the length of the string.Auxiliary Space: O(N2), to store all substrings in hashmap. Comment More infoAdvertise with us Next Article Count of all unique substrings with non-repeating characters S Sanjit_Prasad Follow Improve Article Tags : Strings Greedy Pattern Searching Competitive Programming DSA substring +2 More Practice Tags : GreedyPattern SearchingStrings Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like