Are They Anagrams? Let's Find Out

The Anagram Enigma – Sorting Out the Truth with Java

String problems are often deceptively simple. Take this one: Are two strings just jumbled versions of each other? That’s it. But behind the scenes? There’s plenty to explore.

Today, we’re breaking down Leetcode 242: Valid Anagram – and serving up all the Java solutions from basic to Unicode-ready.

⚡ The Best Data Structure (and Why!)

  • For basic lowercase inputs: a frequency array (int[26]) is fastest and most space-efficient.

  • For Unicode: HashMap<Character, Integer> lets us handle extended character sets.

🔍 Different Approaches

1. Sort and Compare

If two strings are anagrams, their sorted versions will be identical.

import java.util.Arrays; public class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; char[] sArr = s.toCharArray(); char[] tArr = t.toCharArray(); Arrays.sort(sArr); Arrays.sort(tArr); return Arrays.equals(sArr, tArr); } }

2. Frequency Count Using Array

Perfect when all characters are lowercase English letters.

public class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; int[] freq = new int[26]; for (int i = 0; i < s.length(); i++) { freq[s.charAt(i) - 'a']++; freq[t.charAt(i) - 'a']--; } for (int count : freq) { if (count != 0) return false; } return true; } }

3. Unicode-Friendly Using HashMap

Handles all kinds of characters – from emojis to accented letters.

import java.util.HashMap; public class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) return false; HashMap<Character, Integer> map = new HashMap<>(); for (char c : s.toCharArray()) { map.put(c, map.getOrDefault(c, 0) + 1); } for (char c : t.toCharArray()) { if (!map.containsKey(c)) return false; map.put(c, map.get(c) - 1); if (map.get(c) == 0) map.remove(c); } return map.isEmpty(); } }

⚠️ Common Mistakes & Pitfalls

  • Forgetting to check if the string lengths are equal right away

  • Not accounting for character frequency (presence isn’t enough)

  • Overlooking edge cases like empty strings or Unicode characters

📊 Time & Space Complexity Analysis

The sort-and-compare method is simple and clean but comes with a time cost of O(n log n). It also requires O(n) space for the character arrays.

The frequency array solution is the fastest and most memory-efficient for lowercase English letters, running in O(n) time and using constant space, O(1).

The HashMap-based solution supports Unicode and handles a wider range of inputs. It runs in O(n) time but requires O(n) space, depending on the number of unique characters.

🌍 Real-World Applications

  • Spell-checking and word validation

  • Text normalization and preprocessing in NLP pipelines

  • Games like Scrabble, Boggle, or word puzzles

  • Detecting tampering or data mismatch in encoded messages


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries