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

In this problem, we need to find all substrings in a string where there exists another substring at a different location that is an anagram of the first substring. An anagram contains the same characters but in different order.

For example, if the input is s = "abcba", we need to find substrings like "a" (appears at positions 0 and 4), "ab" and "ba" (anagrams of each other), etc.

Algorithm

To solve this problem, we follow these steps ?

  • Generate all possible substrings of each length

  • Group substrings by their sorted character representation (anagram signature)

  • Keep only groups with 2 or more substrings (indicating anagrams exist)

  • Return the result in lexicographic order

Example

Let us see the implementation to understand the concept better ?

from collections import defaultdict

def solve(s):
    result = []
    length = len(s)
    
    # Generate substrings of each possible length
    for i in range(1, length + 1):
        substring_map = defaultdict(list)
        
        # Get all substrings of length i
        for j in range(length - i + 1):
            current_substring = s[j : j + i]
            # Sort characters to create anagram signature
            anagram_key = "".join(sorted(current_substring))
            substring_map[anagram_key].append(current_substring)
        
        # Add substrings that have anagrams (appear 2+ times)
        for key, substrings in substring_map.items():
            if len(substrings) >= 2:
                result.extend(substrings)
    
    return sorted(result)

# Test the function
s = "abcba"
print(solve(s))

The output of the above code is ?

['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba']

How It Works

The algorithm works by creating an anagram signature for each substring. For example:

  • "ab" has signature "ab"

  • "ba" has signature "ab" (same as above)

  • "a" has signature "a"

When multiple substrings share the same signature, they are anagrams of each other. The algorithm groups them together and includes all such substrings in the final result.

Conclusion

This solution efficiently finds all substrings with anagrams by using character sorting as an anagram signature. The time complexity is O(n³ log n) where n is the string length, making it suitable for moderate-sized strings.

Updated on: 2026-03-26T15:12:42+05:30

557 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements