
- 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 find total similarities of a string and its substrings in Python
Suppose we have a string s. We have to find the sum of similarities of string s with each of it's suffixes. Here the similarity between two strings are the length of the longest prefix common to both strings.
So, if the input is like s = "pqpqpp", then the output will be 11 because the suffixes of the string are "pqpqpp", "qpqpp", "pqpp", "qpp", "pp" and "p". The similarities of these substrings with the string "pqpqpp" are 6,0,3,0,1, and 1. So the summation is 6 + 0 + 3 + 0 + 1 + 1 = 11.
To solve this, we will follow these steps −
- length := size of s
- total := length
- z := a list containing 0 initially
- l := 0, r := 0
- for k in range 1 to length - 1, do
- if k > r, then
- match:= 0
- index := k
- while index < length, do
- if s[index] is same as s[match], then
- match := match + 1
- index := index + 1
- otherwise,
- come out from loop
- if s[index] is same as s[match], then
- insert match at the end of z
- if match > 0, then
- total := total + match
- l := k
- r := index-1
- otherwise,
- if z[k-l] < (r-k)+1, then
- insert z[k-l] at the end of z
- total := total + z[k-l]
- otherwise,
- match := r-k
- index := r
- while index < length, do
- if s[index] is same as s[match], then
- match := match + 1
- index := index + 1
- otherwise,
- come out from loop
- if s[index] is same as s[match], then
- insert match at the end of z
- total := total + match
- l := k
- r := index-1
- if z[k-l] < (r-k)+1, then
- if k > r, then
- return total
Example
Let us see the following implementation to get better understanding −
def solve(s): length = len(s) total = length z = [0] l = 0 r = 0 for k in range(1,length): if k > r: match=0 index = k while index < length: if s[index] == s[match]: match +=1 index +=1 else: break z.append(match) if match > 0: total+=match l = k r = index-1 else: if z[k-l] < (r-k)+1: z.append(z[k-l]) total+=z[k-l] else: match = r-k index = r while index < length: if s[index] == s[match]: match +=1 index +=1 else: break z.append(match) total+=match l = k r = index-1 return total s = "pqpqpp" print(solve(s))
Input
"pqpqpp"
Output
11
- Related Articles
- Program to find total sum of all substrings of a number given as string in Python
- Program to find out number of distinct substrings in a given string in python
- Program to find number of different substrings of a string for different queries in Python
- C# Program to find all substrings in a string
- Program to find split a string into the max number of unique substrings in Python
- Program to find all substrings whose anagrams are present in a string in Python
- Program to find sum of beauty of all substrings in Python
- Program to find out the similarity between a string and its suffixes in python
- Lexicographic rank of a string among all its substrings
- Program to find maximum number of non-overlapping substrings in Python
- Program to count substrings with all 1s in binary string in Python
- Sum of similarities of string with all of its suffixes in C++
- Program to print all substrings of a given string in C++
- Program to find out the substrings of given strings at given positions in a set of all possible substrings in python
- Program to find the indexes where the substrings of a string match with another string fully or differ at one position in python

Advertisements