Rearrangement Check Using Letter Tallies, No Sorting
StringsAuto-graded · 22 test casesLast updated
Decide whether two lowercase strings are rearrangements of each other using letter frequency counting in linear time, without sorting.
You are given two strings, first and second, made up only of lowercase English letters (a to z).
Return True if second can be formed by rearranging the letters of first, using every letter of first exactly once. Otherwise return False.
In other words, the two strings must contain exactly the same letters, each appearing the same number of times, though the order may differ. A string is always a rearrangement of itself, and two empty strings count as rearrangements of each other.
Do not sort either string. Aim for a solution that runs in linear time in the length of the strings.
Constraints
- Both strings contain only lowercase English letters
a-z. - Either string may be empty.
- The strings may have different lengths; in that case the answer is
False. - Sorting either string is not allowed.
- Target time complexity: O(n), where n is the string length.
Examples
Example 1
Input: is_rearrangement('listen', 'silent')
Output: True
Example 2
Input: is_rearrangement('rat', 'car')
Output: False
Hints
Hint 1
If the two strings have different lengths, can they possibly be rearrangements of each other? Check this first.
Hint 2
Only 26 distinct letters are possible, so a fixed-size array of counters indexed by letter position can replace a hash map.
Hint 3
Add one to a counter for each letter of the first string, then subtract one for each letter of the second. If a counter ever goes below zero, the second string has too many of that letter.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two strings are rearrangements of each other exactly when every letter occurs the same number of times in both. Sorting would achieve this in O(n log n), but we can do better with counting.
Because the alphabet has just 26 letters, an array of 26 integers works as the frequency table, with letter ch stored at index ord(ch) - ord('a').
- If the lengths differ, return
Falseimmediately. - Walk through
firstand increment the counter of each letter. - Walk through
secondand decrement the counter of each letter. If a counter becomes negative,seconduses that letter more often thanfirstprovides, so returnFalse. - If the second pass finishes without any negative counter, return
True.
Why it is correct
After the first pass the counters sum to len(first). The second pass subtracts len(second) in total, and since the lengths are equal, the counters sum to exactly zero at the end. If none of them ever went negative, they are all non-negative integers summing to zero, so each one is zero. That means every letter was used equally often in both strings.
An early False is also valid: once a counter is negative, later steps only decrement, so it can never recover.
The length check is essential. Without it, "aab" and "ab" would never produce a negative counter and would wrongly return True.
Complexity
- Time: O(n), one pass over each string with constant-time work per character.
- Space: O(1), since the counter array always has 26 entries.
Reference solution
def is_rearrangement(first: str, second: str) -> bool:
if len(first) != len(second):
return False
counts = [0] * 26
for ch in first:
counts[ord(ch) - ord('a')] += 1
for ch in second:
idx = ord(ch) - ord('a')
counts[idx] -= 1
if counts[idx] < 0:
return False
return True