AAlgoLoopSpaced repetition for LeetCode
EASYHash Map / SetLeetCode ↗

Valid Anagram

The key idea

Two strings are anagrams iff every letter appears the same number of times in both. Count the letters of s, then subtract the letters of t; if every count ends at 0 (and the lengths match), t is an anagram of s.

Problem

Given two strings s and t, return true if t is an anagram of s, and false otherwise.

An anagram is a word or phrase formed by rearranging the letters of a different word, using all the original letters exactly once.

Constraints

Examples

Input: s = "anagram", t = "nagaram" Output: true
Input: s = "rat", t = "car" Output: false

Complexity

Time: O(n) Space: O(1)

See the full solution

410310
Step-by-step visualization
Start free →

More Hash Map / Set problems