🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.O(n²)
  2. B.O(n log n) — sorting is required
  3. C.O(n)
  4. 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 →