# Program to find all substrings whose anagrams are present in a string in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a string s with lowercase letters. We have to find all in s where there must be another substring in s at a different location that is an anagram of that taken substrings. We have to find a list of substrings in in lexicographic order.

So, if the input is like s = "abcba", then the output will be ['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba'] for each of them we can find different anagrams present in the string itself.

To solve this, we will follow these steps −

• res := a new list

• L := size of s

• for i in range 1 to L, do

• smap := an empty dictionary, and all values are of type list

• for j in range 0 to L - i, do

• cs := substring of s from index j to j + i-1]

• k := string after joining items of cs in sorted form

• insert cs at the end of smap[k]

• for each key k and value v in smap, do

• if size of v >= 2, then

• insert elements of v into res

• return res after sorting

## Example

Let us see the following implementation to get better understanding

from collections import defaultdict
def solve(s):
res = []
L = len(s)
for i in range(1, L + 1):
smap = defaultdict(list)
for j in range(L - i + 1):
cs = s[j : j + i]
k = "".join(sorted(cs))
smap[k].append(cs)
for k, v in smap.items():
if len(v) >= 2:
res.extend(v)

return sorted(res)

s = "abcba"
print(solve(s))

## Input

"abcba"


## Output

['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba']
Updated on 11-Oct-2021 07:22:40