EasyHash TableStringSorting

Valid Anagram

LeetCode
2 approaches, code in all languages

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
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now