Decide whether two strings are anagrams with a character count in O(n), and see why sorting both is O(n log n). Run Python tests in your browser.
The problem
Return True if s1 and s2 are anagrams — the same characters with the same counts, in any order. Comparison is case-sensitive.
Examples
Example 1
Input
is_anagram('listen', 'silent')Expected output
True
Example 2
Input
is_anagram('rat', 'car')Expected output
False
+ 4 hidden tests on Submit — same letters, different counts, different length, case-sensitive.
Edge cases to ask about
- Empty strings
- Different lengths
- Case differences
- Unicode
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Sort both | O(n log n) | O(n) | Two sorted copies compared. |
| bestCount characters | O(n) | O(k) | Increment for s1, decrement for s2; any shortfall means no. |
Walkthrough of the optimal approach (try it yourself first)
Count characters in s1, then decrement for each character in s2. If a count would go below zero, the strings differ. A quick length check first makes the "same counts" logic complete.
sorted(s1) == sorted(s2) is fine to say out loud — then point out it is O(n log n) and the count version is O(n).
Complexity: O(n) time, O(k) space. Each string is walked once; the count table has one slot per distinct character.
Reveal the reference solution
def is_anagram(s1, s2): if len(s1) != len(s2): return False counts = {} for ch in s1: counts[ch] = counts.get(ch, 0) + 1 for ch in s2: if counts.get(ch, 0) == 0: return False counts[ch] -= 1 return True
The brute force, for comparison
def is_anagram(s1, s2): return sorted(s1) == sorted(s2)
Follow-ups interviewers ask
- Ignore spaces and case.
- Group a list of words into anagram groups (question 28).
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Check If Two Strings Are Anagrams in Python?
The optimal solution runs in O(n) time and O(k) auxiliary space. Each string is walked once; the count table has one slot per distinct character.
What is the brute-force approach, and how do you optimise it?
Sort both: O(n log n) time, O(n) space. Two sorted copies compared. Count characters: O(n) time, O(k) space. Increment for s1, decrement for s2; any shortfall means no.
What follow-up questions do interviewers ask about Check If Two Strings Are Anagrams?
Ignore spaces and case. Group a list of words into anagram groups (question 28).
