

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- Count the Number of matching characters in a pair of string in Python
- Program to count number of distinct substrings in s in Python
- Count number of substrings with exactly k distinct characters in C++
- Count the Number of matching characters in a pair of Java string
- Program to count number of unique palindromes we can make using string characters in Python
- Program to count number of characters in each bracket depth in Python
- How to count the number of repeated characters in a Golang String?
- Python Program to Calculate the Number of Words and the Number of Characters Present in a String
- Program to find out number of distinct substrings in a given string in python
- Python program to count distinct words and count frequency of them