Skip to main content

Check Two Words Are Anagrams

EasyStringsHash mapSorting223 tests

You get two strings, s and t. Both contain only lowercase English letters.

Return true if t uses exactly the same letters as s, each letter the same number of times, only in a different order (or the same order). Otherwise return false.

Hint

Count how many times each letter appears in each string, and compare the counts.

Examples

Example 1

Input:
"lemon", "melon"
Output:
true

Both words use the letters e, l, m, n, o once each.

Example 2

Input:
"kite", "kit"
Output:
false

The words have different lengths, so they cannot use the same letters.

Example 3

Input:
"aabb", "abbb"
Output:
false

Both words use only a and b, but s has two a letters and t has only one.

Constraints

  • 1 <= s.length, t.length <= 15
  • s and t contain only lowercase English letters.

Run checks your code against the 3 examples. Submit checks it against all 223 tests and needs a free account. You can also try ideas in the free JavaScript or Python compiler.