Text permutation detection involves determining if two strings are composed of the exact same characters with the same frequencies, just arranged differently (often called anagrams). This is a fundamental problem in algorithm design, cryptography, and data validation.
This guide explores the most efficient methods to detect permutations in Python, ranging from simple sorting to high-performance frequency counting using the standard library.
Understanding Text Permutations
Two strings are permutations of each other if:
- They have the same length.
- They contain the same characters.
- Each character appears the same number of times.
Examples:
"listen"and"silent"-> True"triangle"and"integral"-> True"hello"and"world"-> False"aabb"and"ab"-> False (Different counts)
Method 1: Sorting (The Easiest Approach)
The logic here is simple: if two strings contain the same characters, sorting them alphabetically should result in identical strings.
Complexity: O(N log N) due to the sorting algorithm.
def is_permutation_sorted(str1, str2):
if len(str1) != len(str2):
return False
return sorted(str1) == sorted(str2)
s1 = "listen"
s2 = "silent"
s3 = "listens" # Extra 's'
print(f"'{s1}' vs '{s2}': {is_permutation_sorted(s1, s2)}")
print(f"'{s1}' vs '{s3}': {is_permutation_sorted(s1, s3)}")Output:
'listen' vs 'silent': True
'listen' vs 'listens': FalseMethod 2: Using Hash Maps (The Fastest Approach)
To improve performance to O(N) (linear time), we can count the frequency of each character. Python's collections.Counter is the standard tool for this.
Complexity: O(N) time, O(K) space (where K is the character set size).
Using collections.Counter
from collections import Counter
def is_permutation_counter(str1, str2):
if len(str1) != len(str2):
return False
return Counter(str1) == Counter(str2)
print(f"Test 1: {is_permutation_counter('triangle', 'integral')}")
print(f"Test 2: {is_permutation_counter('apple', 'papel')}")Output:
Test 1: True
Test 2: TrueManual Dictionary Implementation
If you cannot import modules, you can implement the frequency logic manually.
def is_permutation_manual(str1, str2):
if len(str1) != len(str2):
return False
counts = {}
for char in str1:
counts[char] = counts.get(char, 0) + 1
for char in str2:
if char not in counts:
return False
counts[char] -= 1
if counts[char] < 0:
return False
return TrueMethod 3: Bit Vector (Memory Optimized for ASCII)
If you are strictly working with ASCII characters and only care about whether characters exist (ignoring duplicate counts) or have simpler constraints, you can use bit manipulation.
def is_permutation_bitvector(str1, str2):
checker1 = 0
checker2 = 0
for char in str1:
val = ord(char)
checker1 |= (1 << val)
for char in str2:
val = ord(char)
checker2 |= (1 << val)
return checker1 == checker2
print(f"Set match: {is_permutation_bitvector('abc', 'cba')}")Output:
Set match: TrueConclusion
To detect text permutations in Python:
- Use
collections.Counter(Counter(s1) == Counter(s2)) for the most efficient, standard approach (O(N)). - Use
sorted()(sorted(s1) == sorted(s2)) for short strings where code readability is the priority (O(N log N)). - Check Lengths First: Always compare
len(str1) != len(str2)immediately to returnFalsequickly.