Given a string str, the task is to find the sum of the similarities of str with each of its suffixes.
The similarity of strings A and B is the length of the longest prefix common to both the strings i.e. the similarity of “aabc” and “aab” is 3 and that of “qwer” and “abc” is 0.
Input: str = “ababa”
The suffixes of str are “ababa”, “baba”, “aba”, “ba” and “a”. The similarities of these strings with the original string “ababa” are 5, 0, 3, 0 & 1 respectively.
Thus, the answer is 5 + 0 + 3 + 0 + 1 = 9.
Input: str = “aaabaab”
Approach: Compute Z-array using Z-algorithm – For a string str[0..n-1], Z array is of same length as string. An element Z[i] of Z array stores length of the longest substring starting from str[i] which is also a prefix of str[0..n-1]. The first entry of Z array is the length of the string.
Now, sum all the elements of the Z-array to get the required sum of the similarities.
Below is the implementation of the above approach:
- Concatenate suffixes of a String
- Pattern Searching using a Trie of all Suffixes
- Sum of decomposition values of all suffixes of an Array
- Difference and similarities between HashSet, LinkedHashSet and TreeSet in Java
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Minimal moves to form a string by adding characters or appending string itself
- Create a new string by alternately combining the characters of two halves of the string in reverse
- String Range Queries to find the number of subsets equal to a given String
- Longest palindromic string formed by concatenation of prefix and suffix of a string
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- String slicing in Python to check if a string can become empty by recursive deletion
- Transform string str1 into str2 by taking characters from string str3
- Check if a string can be converted to another string by replacing vowels and consonants
- Find the count of palindromic sub-string of a string in its sorted form
- Check if given string is a substring of string formed by repeated concatenation of z to a
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Minimum deletions from string to reduce it to string with at most 2 unique characters
- Generate a string whose all K-size substrings can be concatenated to form the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.