← Arrays & Hashing Quiz: all 8 questions
Arrays & Hashing Quiz · Question 5 of 8
What is the best time complexity to check whether two strings are anagrams?
What is the best time complexity to check whether two strings are anagrams?
- A.O(n²)
- B.O(n log n) — sorting is required
- C.O(n)
- D.O(1)
Answer & explanation
Correct answer: O(n)
Count characters in one pass (a 26-length array or a hash map) and compare counts. Sorting works but is O(n log n).
Can you get the rest right without peeking?
Take the timed Arrays & Hashing Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →