2dbi

Check if two words are anagrams

viaLeetCode

Problem Given two words, determine whether one is an anagram of the other (same characters with the same multiplicities, order ignored).

Input / Output

  • Input: strings a and b.
  • Output: boolean.

Constraints

  • Lengths up to 10^5; clarify case sensitivity, Unicode, and whether spaces/punctuation count.

Example

  • "listen" / "silent" → true; "rat" / "car" → false.

Expected approach

  • Length check, then count characters: a fixed 26-slot array (lowercase ASCII) incremented for a and decremented for b, verifying all zeros — O(n) time, O(1) space. Hash map for general alphabets. Sorting both strings and comparing is the simpler O(n log n) alternative — mention both and pick counting.
Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account