
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to count number of distinct characters of every substring of a string in Python
Suppose we have a lowercase string s, we have to find the sum of the count of characters that are distinct in every substring of s. If the answer is very large then return result mod 10^9+7.
So, if the input is like s = "xxy", then the output will be 6, as the substrings and their counts are −
"x" : 1
"x" : 1
"y" : 1
"xx" : 0 (as "x" is not distinct)
"xy" : 2
"xxy" : 1 ("as "x" is not distinct)
To solve this, we will follow these steps −
m := 10^9 + 7
prev_seen := a new empty map
ans := 0
Define a function util() . This will take i, symbol
prev_seen[symbol] := a list with single value −1
prev := prev_seen[symbol]
insert i at the end of prev
if size of prev > 2, then
left := first element of prev and delete first element from prev
middle := prev[0], right := prev[1]
cnt :=(middle − left) *(right − middle)
ans :=(ans + cnt) mod m
for each index i and value symbol in s, do
util(i, symbol)
for each symbol in prev_seen, do
util(size of s , symbol)
return ans
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, s): m = 10 ** 9 + 7 prev_seen = {} ans = 0 def util(i, symbol): nonlocal ans prev = prev_seen.setdefault(symbol, [−1]) prev.append(i) if len(prev) > 2: left = prev.pop(0) middle, right = prev cnt = (middle − left) * (right − middle) ans = (ans + cnt) % m for i, symbol in enumerate(s): util(i, symbol) for symbol in prev_seen: util(len(s), symbol) return ans ob = Solution() s = "xxy" print(ob.solve(s))
Input
xxy
Output
6
- Related Articles
- Count number of Distinct Substring in a String in C++
- Python program to count number of substring present in string
- Python Program to Count Number of Lowercase Characters in a String
- Count Number of Lowercase Characters in a String in Python Program
- Program to find length of longest substring which contains k distinct characters in Python
- Program to count number of distinct substrings in s in Python
- Count the Number of matching characters in a pair of string in Python
- Find distinct characters in distinct substrings of a string
- Program to count number of unique palindromes we can make using string characters in Python
- Count number of substrings with exactly k distinct characters in C++
- Program to count number of characters in each bracket depth in Python
- Program to find out number of distinct substrings in a given string in python
- Python program to count the number of spaces in string
- Program to count number of palindromes of size k can be formed from the given string characters in Python
- Count the Number of matching characters in a pair of Java string
