16. Valid Anagram

16. Valid Anagram

Easy
62.0%
2.0k
stringhash-tablesorting
Google, Amazon, Netflix, Meta (Facebook), Apple, Microsoft, Uber, Bloomberg, Airbnb, Stripe, Adobe, Salesforce, LinkedIn, Oracle

You’re interviewing for a backend or data engineering role at a FAANG-level company. The interviewer describes a real product scenario:

A large platform processes text-based identifiers such as usernames, search queries, tags, or document keys.
To detect duplicates, spam, or content variations, the system sometimes needs to verify whether two strings are composed of the same characters with the same frequencies, even if the order of characters is different.

These two strings are stored as s and t.
The platform wants a fast and reliable way to check whether one string is an anagram of the other.

This is a common practice coding problem for DSA question might ask in Google, Amazon, Netflix, Meta, Apple, Microsoft, Uber, and Bloomberg interviews.

Your Task

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

An anagram uses the same characters with the same frequencies, but possibly in a different order.

Input Format

  • s: a string
  • t: a string

Output Format

  • A boolean value:
    • true if t is an anagram of s
    • false otherwise

Examples

Example 1:

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

Example 2:

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

Example 3:

Input: s = "a" t = "a"
Output: true

Constraints

  • 1 <= s.length, t.length <= 50,000
  • Strings consist of lowercase English letters only

Loading editor...

"anagram", "nagaram"
true

Console Output

Click "Run Code" or "Submit" to see results

Your code will be evaluated by AI with instant feedback