EasyHash TableStringSorting

2 approaches, code in every language

Problem description

Given two strings, decide whether the second is an anagram of the first. Two strings qualify as anagrams when they have the same length and every character occurs the same number of times in both.

Return true when the second string is a rearrangement of the first, and false otherwise.

Example 1

Input: s = "listen", t = "silent"

Output: true

Both strings use the letters e, i, l, n, s, t exactly once, so one is a rearrangement of the other.

Example 2

Input: s = "hello", t = "world"

Output: false

The letter counts differ (for instance 'l' appears twice in "hello" but once in "world"), so they are not anagrams.

Constraints

  • 1 <= s.length <= 5 * 10^4
  • 1 <= t.length <= 5 * 10^4
  • s and t consist of lowercase English letters

After the pattern

Now solve one you haven't seen.

Matching Letter Tally is our own problem on the technique behind Valid Anagram: a new statement, so there's no answer to remember. Write it in your browser and run it against graded tests.

1,194 problems · 15,062 graded tests · free