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

PythonServer Side ProgrammingProgramming

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


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))
      for k, v in smap.items():
         if len(v) >= 2:

   return sorted(res)

s = "abcba"




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