Valid Anagram
Valid Anagram 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.
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
- All coding practice problems
Problems related to Valid Anagram
Other problems that use the same Anagram Check (Frequency Count/Sort) and String Manipulation Patterns techniques:
- Group Anagrams — Medium, same subpattern
- Two Sum — Easy, same subpattern
- Add Binary — Easy, same pattern
- Add to Array-Form of Integer — Easy, same pattern
- Diagonal Traverse — Medium, same pattern
- Find All Duplicates in an Array — Medium, same pattern
Preparing your workspace
Progress is saved as you study.