AlgoInsight

DSA problem

Valid Anagram

EasyStringsPattern: Frequency Counttop600AmazonGoogleFacebookBloomberg

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 or phrase, using all the original letters exactly once. Two strings are anagrams if and only if they have the same length and the same multiset of characters.

For example, "anagram" and "nagaram" are anagrams, while "rat" and "car" are not because their character counts differ.

Examples

Example 1
Input: s = "anagram", t = "nagaram"
Output: true
Explanation: Both strings contain the letters a x3, n x1, g x1, r x1, m x1, so t is a rearrangement of s.
Example 2
Input: s = "rat", t = "car"
Output: false
Explanation: s contains a t while t contains a c, so the character counts do not match.

Constraints

3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.

stringhash-tablesortingfrequency-count

Browse all DSA problems