Weekly Coding Challenge #4
Given two strings s and t, return true if t is an anagram of s (same letters, same counts, any order), otherwise false.
Examples
s = "anagram", t = "nagaram"→trues = "rat", t = "car"→false
Constraints
1 <= s.length, t.length <= 5 * 10^4sandtconsist of lowercase English letters.
Follow-up: what if the inputs contain Unicode characters? How would you adapt your solution?
Why this pattern matters
Counting item frequencies is everywhere: bag-of-words and token counts in NLP, near-duplicate and plagiarism detection, log analysis, and fraud checks. Frequency maps turn "are these the same?" into a cheap comparison.
Solution to Challenge #3: Contains Duplicate
Problem: given an integer array nums, return true if any value appears at least twice, otherwise false.
Approach: brute force compares every pair, O(n²). Instead, scan once and remember what you've seen in a HashSet. add() returns false when the value is already present, so the first repeat ends the search.
import java.util.HashSet;
import java.util.Set;
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int num : nums) {
if (!seen.add(num)) {
return true;
}
}
return false;
}
}