DSA Problems/πŸ“ŠArrays & Hashing

Valid Anagram

Hash TableStringSorting

Problem Statement

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

An anagram is a word formed by rearranging the letters of another word using all the original letters exactly once.

Approach

Count character frequencies in both strings and compare. Use a hash map or array of size 26.

Complexity Analysis

MetricValue
TimeO(n)
SpaceO(1) - constant 26 characters

Examples

Example 1

Input: s = "anagram", t = "nagaram" Output: true

Example 2

Input: s = "rat", t = "car" Output: false

Constraints

  • β–Έ1 <= s.length, t.length <= 5 * 10^4
  • β–Έs and t consist of lowercase English letters
Loading...
Sign in to run your code...

Asked by companies:

GoogleAmazonMicrosoftBloomberg