242. Valid Anagram
Valid Anagram (LeetCode 242) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the String Manipulation Patterns pattern, in the Anagram Check (Frequency Count/Sort) subpattern.
- Blind 75 · #2 of 75 · Arrays & Hashing
- NeetCode 150 · #2 of 150 · Arrays & Hashing
- NeetCode 250 · #3 of 250 · Arrays & Hashing
How to solve Valid Anagram
Valid Anagram is a Anagram Check problem, part of String Manipulation Patterns. Master anagram detection using frequency counting and sorting. Group anagrams efficiently.
Work towards the solution in these steps:
- Think about what property defines two strings as anagrams of each other.
- Consider how you can represent the frequency of each character in both strings.
- Try using a data structure that can efficiently count and compare character occurrences.
The reference solution runs in O(n) time and O(1) space.
The Valid Anagram solution and editorial walks through the full approach with reference code.
Problem statement
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
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 104
- s and t consist of lowercase English letters.
Follow up: What if the inputs contain Unicode characters? How would you adapt your solution to such a case?
Topics and companies
Topics: Hash Table, String, Sorting.
Reported in interviews at Bloomberg, Facebook, Microsoft, Amazon, Apple, Oracle, Google, Goldman Sachs, Adobe, Cisco, Paypal, Qualcomm.
How to practise Valid Anagram on Thita.ai
Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.
Open Valid Anagram in the code editor, or read the Valid Anagram editorial for a worked solution with its approach and complexity analysis.
Where Valid Anagram sits in the DSA pattern sheet
- String Manipulation Patterns pattern guide
- Anagram Check (Frequency Count/Sort) subpattern
- The full DSA patterns sheet
Problems related to Valid Anagram
Other problems that use the same Anagram Check (Frequency Count/Sort) and String Manipulation Patterns techniques:
- Find the Town Judge — Easy, same subpattern
- Group Anagrams — Medium, same subpattern
- Majority Element — Easy, same subpattern
- Majority Element II — Medium, same subpattern
- Subarray Sum Equals K — Medium, same subpattern
- Two Sum — Easy, same subpattern
Valid Anagram FAQ
What is the LeetCode number of Valid Anagram?
Valid Anagram is LeetCode problem 242, rated Easy.
What pattern is used to solve Valid Anagram?
Valid Anagram is a Anagram Check problem. It belongs to the String Manipulation Patterns pattern, in the Anagram Check (Frequency Count/Sort) subpattern.
What is the time complexity of Valid Anagram?
The reference solution runs in O(n) time and O(1) space.
Which companies ask Valid Anagram?
Valid Anagram has been reported in interviews at Bloomberg, Facebook, Microsoft, Amazon, Apple, Oracle, Google, Goldman Sachs, Adobe, Cisco, Paypal, Qualcomm.