Command Palette
Search for a command to run...
Problem 4.2 · HashingEasy
What it teaches: Compare two strings by letter counts with a 26-slot array instead of sorting.
Practise it on judges as “Valid Anagram”.
The problem
Given two strings s and t, return true if t is an anagram of s: it uses exactly the same letters, the same number of times.
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⁴
- Lowercase English letters
Pattern clues in the wording
- → Same letters, any order
- → Small fixed alphabet (26 letters)
These clues point to Frequency Counting: Count how many times each value appears (with an int[26] or a HashMap), then answer from the counts.
Stuck? Take one hint at a time
Solution.java · starterclass Solution {
public boolean isAnagram(String s, String t) {
int[] count = new int[26];
return true;
}
}
Write your solution locally or in your editor for now. The in-browser runner (Java first, then Python, C++ and more) will run these tests right here.
Test cases
| # | Input | Expected |
|---|
| 1 | s = "anagram" t = "nagaram" | true |
| 2 | s = "rat" t = "car" | false |
+ 1 hidden test the code runner will check