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:6Explanation:

All possible substrings from the given string are,

( “g“, “gf“, “gff”, “gffg”, “f“, “ff”, “ffg”, “f“, “fg“, “g” )

Among them, the highlighted ones consists of distinct characters only.

Input:str = “gfg”Output:5Explanation:

All possible substrings from the given string are,

( “g“, “gf“, “gfg”, “f“, “fg“, “g” )

Among them, the highlighted consists of distinct characters only.

**Naive Approach:**

The simplest approach is to generate all possible substrings from the given string and check for each substring whether it contains all distinct characters or not. If the length of string is **N**, then there will be **N*(N+1)/2** possible substrings. **Time complexity:** O(N^{3})**Auxiliary Space:** O(1)

**Efficient Approach:**

The problem can be solved in linear time using Two Pointer Technique, with the help of counting frequencies of characters of the string.

Detailed steps for this approach are as follows:

- Consider two pointers
**i**and**j**, initially both pointing to the first character of the string i.e.**i = j = 0**. - Initialize an array
**Cnt[ ]**to store the count of characters in substring from index**i**to**j**both inclusive. - Now, keep on incrementing
**j**pointer until some a repeated character is encountered. While incrementing**j**, add the count of all the substrings ending at**j**^{th}index and starting at any index between**i**and**j**to the answer. All these substrings will contain distinct characters as no character is repeated in them. - If some repeated character is encountered in substring between index
**i**to**j**, to exclude this repeated character, keep on incrementing the**i**pointer until repeated character is removed and keep updating**Cnt[ ]**array accordingly. - Continue this process until
**j**reaches to the end of string. Once the string is traversed completely, print the answer.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement ` `// the above appraoach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count total ` `// number of valid substrings ` `long` `long` `int` `countSub(string str) ` `{ ` ` ` `int` `n = (` `int` `)str.size(); ` ` ` ` ` `// Stores the count of ` ` ` `// substrings ` ` ` `long` `long` `int` `ans = 0; ` ` ` ` ` `// Stores the frequency ` ` ` `// of characters ` ` ` `int` `cnt[26]; ` ` ` ` ` `memset` `(cnt, 0, ` `sizeof` `(cnt)); ` ` ` ` ` `// Initialised both pointers ` ` ` `// to beginning of the string ` ` ` `int` `i = 0, j = 0; ` ` ` ` ` `while` `(i < n) { ` ` ` ` ` `// If all characters in ` ` ` `// substring from index i ` ` ` `// to j are distinct ` ` ` `if` `(j < n && (cnt[str[j] - ` `'a'` `] ` ` ` `== 0)) { ` ` ` ` ` `// Increment count of j-th ` ` ` `// character ` ` ` `cnt[str[j] - ` `'a'` `]++; ` ` ` ` ` `// Add all substring ending ` ` ` `// at j and starting at any ` ` ` `// index between i and j ` ` ` `// to the answer ` ` ` `ans += (j - i + 1); ` ` ` ` ` `// Increment 2nd pointer ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// If some characters are repeated ` ` ` `// or j pointer has reached to end ` ` ` `else` `{ ` ` ` ` ` `// Decrement count of j-th ` ` ` `// character ` ` ` `cnt[str[i] - ` `'a'` `]--; ` ` ` ` ` `// Increment first pointer ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final ` ` ` `// count of substrings ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `string str = ` `"gffg"` `; ` ` ` ` ` `cout << countSub(str); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above appraoach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to count total ` `// number of valid subStrings ` `static` `int` `countSub(String str) ` `{ ` ` ` `int` `n = (` `int` `)str.length(); ` ` ` ` ` `// Stores the count of ` ` ` `// subStrings ` ` ` `int` `ans = ` `0` `; ` ` ` ` ` `// Stores the frequency ` ` ` `// of characters ` ` ` `int` `[]cnt = ` `new` `int` `[` `26` `]; ` ` ` ` ` `// Initialised both pointers ` ` ` `// to beginning of the String ` ` ` `int` `i = ` `0` `, j = ` `0` `; ` ` ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// If all characters in ` ` ` `// subString from index i ` ` ` `// to j are distinct ` ` ` `if` `(j < n && ` ` ` `(cnt[str.charAt(j) - ` `'a'` `] == ` `0` `)) ` ` ` `{ ` ` ` ` ` `// Increment count of j-th ` ` ` `// character ` ` ` `cnt[str.charAt(j) - ` `'a'` `]++; ` ` ` ` ` `// Add all subString ending ` ` ` `// at j and starting at any ` ` ` `// index between i and j ` ` ` `// to the answer ` ` ` `ans += (j - i + ` `1` `); ` ` ` ` ` `// Increment 2nd pointer ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// If some characters are repeated ` ` ` `// or j pointer has reached to end ` ` ` `else` ` ` `{ ` ` ` ` ` `// Decrement count of j-th ` ` ` `// character ` ` ` `cnt[str.charAt(i) - ` `'a'` `]--; ` ` ` ` ` `// Increment first pointer ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final ` ` ` `// count of subStrings ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String str = ` `"gffg"` `; ` ` ` ` ` `System.out.print(countSub(str)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above appraoach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to count total ` `// number of valid subStrings ` `static` `int` `countSub(String str) ` `{ ` ` ` `int` `n = (` `int` `)str.Length; ` ` ` ` ` `// Stores the count of ` ` ` `// subStrings ` ` ` `int` `ans = 0; ` ` ` ` ` `// Stores the frequency ` ` ` `// of characters ` ` ` `int` `[]cnt = ` `new` `int` `[26]; ` ` ` ` ` `// Initialised both pointers ` ` ` `// to beginning of the String ` ` ` `int` `i = 0, j = 0; ` ` ` ` ` `while` `(i < n) ` ` ` `{ ` ` ` ` ` `// If all characters in ` ` ` `// subString from index i ` ` ` `// to j are distinct ` ` ` `if` `(j < n && ` ` ` `(cnt[str[j] - ` `'a'` `] == 0)) ` ` ` `{ ` ` ` ` ` `// Increment count of j-th ` ` ` `// character ` ` ` `cnt[str[j] - ` `'a'` `]++; ` ` ` ` ` `// Add all subString ending ` ` ` `// at j and starting at any ` ` ` `// index between i and j ` ` ` `// to the answer ` ` ` `ans += (j - i + 1); ` ` ` ` ` `// Increment 2nd pointer ` ` ` `j++; ` ` ` `} ` ` ` ` ` `// If some characters are repeated ` ` ` `// or j pointer has reached to end ` ` ` `else` ` ` `{ ` ` ` ` ` `// Decrement count of j-th ` ` ` `// character ` ` ` `cnt[str[i] - ` `'a'` `]--; ` ` ` ` ` `// Increment first pointer ` ` ` `i++; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the final ` ` ` `// count of subStrings ` ` ` `return` `ans; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `String str = ` `"gffg"` `; ` ` ` ` ` `Console.Write(countSub(str)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

6

**Time complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Find distinct characters in distinct substrings of a string
- Count number of substrings with exactly k distinct characters
- Count distinct substrings that contain some characters at most k times
- Count of substrings of length K with exactly K distinct characters
- Generate a String of having N*N distinct non-palindromic Substrings
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count all substrings having character K
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Replace minimal number of characters to make all characters pair wise distinct
- Count of all unique substrings with non-repeating characters
- Count of substrings of a given Binary string with all characters same
- Contiguous subsegments of a string having distinct subsequent characters
- Find the String having each substring with exactly K distinct characters
- Count of distinct substrings of a string using Suffix Trie
- Count of distinct substrings of a string using Suffix Array
- Count number of distinct substrings of a given length
- Count distinct substrings of a string using Rabin Karp algorithm
- Count of Distinct Substrings occurring consecutively in a given String
- Minimum changes to a string to make all substrings distinct

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.