
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to find out number of distinct substrings in a given string in python
Suppose, we are given a substring denoted by 's'. We have to find out the unique substrings and return the number of these substrings as output.
So, if the input is like s = 'prrstvt', then the output will be 26.
The distinct substrings will be −
'pr', 'rrs', 'st', 'rr', 'tv', 'rstv', 'stvt', 'prrstv', 'prrstvt', 'rrstvt', 's', 'prrst', 'stv', 'rrstv', 'rst', 'v', 'tvt', 'rstvt', 'r', 'rs', 'vt', 't', 'prr', 'p', 'rrst', and 'prrs'.
To solve this, we will follow these steps −
- visited := a new map
- for each index ind, and value let in s, do
- temp := a new set
- if ind-1 is present in visited, then
- for each has_let in visited[ind-1], do
- add(has_let + let) to list temp
- for each has_let in visited[ind-1], do
- add(let) to list temp
- visited[ind] := temp
- res := a new set
- for each sets in visited, do
- add(visited[sets]) to res
- return size of res
Example
Let us see the following implementation to get better understanding −
def solve(s): visited = dict() for ind, let in enumerate(s): temp = set() if ind-1 in visited: for has_let in visited[ind-1]: temp.add(has_let+let) temp.add(let) visited[ind] = temp res = set() for sets in visited: res.update(visited[sets]) return len(res) print(solve('prrstvt'))
Input
'prrstvt'
Output
26
- Related Articles
- Program to find total sum of all substrings of a number given as string in Python
- Program to count number of distinct substrings in s in Python
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Program to find out the number of pairs of equal substrings in Python
- Program to find number of different substrings of a string for different queries in Python
- Program to find split a string into the max number of unique substrings in Python
- Program to find number of distinct island shapes from a given matrix in Python
- C++ Program to find out the distinct elements in a given sequence
- Program to find number of distinct subsequences in Python
- Program to print all substrings of a given string in C++
- Python program to find N-sized substrings with K distinct characters
- Program to find total similarities of a string and its substrings in Python
- Program to find out the number of special numbers in a given range in Python
- Program to find maximum number of non-overlapping substrings in Python
- Find all distinct palindromic sub-strings of a given String in Python

Advertisements