RegEx and Anagrams
Filed under Optum (UnitedHealth Group)
Free read 2 of 3Determine if spaces and capitalization matter for the anagram problem.
The question
Write code to solve string manipulation problems involving regular expressions and anagram verification.
Constraints
- Strings consist of lowercase English letters or standard regex wildcard characters.
- 1 <= length of strings <= 10^4
Examples
- Input
- s1 = "listen", s2 = "silent"
- Output
- true
- Why
- Both strings contain the exact same character frequencies, making them anagrams.
Approach
Determine if spaces and capitalization matter for the anagram problem.
Sort both strings and compare them for equality (O(N log N)).
Use a hash map or frequency array of size 26 to count character occurrences in the first string and decrement using the second string.
Different lengths immediately return false.
Solution
O(N) time, O(1) space
from collections import Counter
def is_anagram(s1, s2):
if len(s1) != len(s2):
return False
return Counter(s1) == Counter(s2)
Tips
- What they are testing: Test edge cases like empty strings, strings of different lengths, and case sensitivity.
- Common mistake: Using inefficient sorting for anagrams instead of character frequency counting (O(N) vs O(N log N)).
Related Optum (UnitedHealth Group) questions
Collected from public candidate posts and coding forums. Updated Nov 2026.