DDileep OS
AboutProjectsJourneyBlogBooksContact
Available
--:--
DDileep OS

Building products, learning in public and becoming a better software engineer every day.

Available for opportunitiesv1.0.0

Explore

HomeAboutProjectsJourneyBlogContact

Resources

BooksLearningDSAJavaUsesResume

More

ExperienceAchievementsNowGuestbook

Connect

Follow my work, connect professionally, or drop me an email anytime.

© 2026 Dileep OS · Built with Next.js, Sanity and Tailwind CSS.

Back to problemsSolve on LeetCode
MediumLeetCodeSolved

Valid Anagram

HashMap is useful for counting character frequencies efficiently. Instead of sorting both strings, counting occurrences allows us to compare them in a single traversal. Always check the string lengths first because strings of different lengths can never be anagrams. This frequency-counting technique is widely used in hashing and string interview problems.

StringHashMap

Problem stats

Solved on7/15/2026
Attempts1
Time complexityO(n)
Space complexityO(n)
Solution strategy

From brute force to optimal

Before

Brute force

If the lengths of both strings are different, they cannot be anagrams.

Otherwise:
1. Convert the second string into a character array.
2. For every character in the first string, search for the same character in the second array.
3. If found, mark it as used.
4. If any character cannot be found, return false.
5. If all characters are matched, return true.

This solution repeatedly searches through the string, making it inefficient.

VS

After

Optimal

If both strings have different lengths, return false immediately.

Use a HashMap to count the frequency of each character in the first string.
Then traverse the second string and decrease the count.

If any character is missing or its count becomes negative, return false.

If every frequency becomes zero after processing both strings, the strings are valid anagrams.

Java Implementation

Complete Java Solution

The optimized implementation, ready for interview preparation and quick revision.

41Lines
28Code Lines
951Characters

Solution.java

Java
1import java.util.HashMap;
2import java.util.Map;
3
4public class Solution {
5
6 public boolean isAnagram(String s, String t) {
7
8 if (s.length() != t.length()) {
9 return false;
10 }
11
12 Map<Character, Integer> frequency = new HashMap<>();
13
14 for (char ch : s.toCharArray()) {
15 frequency.put(ch, frequency.getOrDefault(ch, 0) + 1);
16 }
17
18 for (char ch : t.toCharArray()) {
19
20 if (!frequency.containsKey(ch)) {
21 return false;
22 }
23
24 frequency.put(ch, frequency.get(ch) - 1);
25
26 if (frequency.get(ch) == 0) {
27 frequency.remove(ch);
28 }
29 }
30
31 return frequency.isEmpty();
32 }
33
34 public static void main(String[] args) {
35
36 Solution solution = new Solution();
37
38 System.out.println(solution.isAnagram("anagram", "nagaram"));
39 System.out.println(solution.isAnagram("rat", "car"));
40 }
41}
41 lines951 chars
Complexity analysis

Performance breakdown

Analyze the efficiency of the algorithm by understanding its time and space complexity across different execution scenarios.

Time complexity

O(n)

Space complexity

O(n)

By execution case

Best case

O(n)

Average case

O(n)

Worst case

O(n)

Complexity summary

This solution achieves a O(n) time complexity while using O(n) extra memory. It is considered the optimal approach for this problem and is suitable for coding interviews as well as competitive programming.

Key Learning

What You Should Remember

Every coding problem teaches a pattern. Focus on the concepts, avoid common mistakes, and remember the interview-worthy takeaways instead of memorizing code.

Core Learning

The biggest takeaway from this problem.

HashMap is useful for counting character frequencies efficiently. Instead of sorting both strings, counting occurrences allows us to compare them in a single traversal. Always check the string lengths first because strings of different lengths can never be anagrams. This frequency-counting technique is widely used in hashing and string interview problems.

Interview Tip

Explain why the optimized solution works before writing the final code. Interviewers care about your thinking process as much as your implementation.

Common Mistake

Avoid jumping directly to coding. Always analyze edge cases, constraints, and the optimal approach before implementation.

Revision Note

Focus on understanding the algorithm's pattern instead of memorizing the code. Once the logic becomes clear, implementing the solution in any programming language becomes much easier.

Final Takeaway

Every DSA problem introduces a reusable pattern. Instead of remembering the exact solution, remember the thought process that led to it. Over time, these patterns will help you solve new problems much faster and perform better in coding interviews.

Learn the pattern, not the code.

Continue learning

Practice consistently and move through the roadmap one problem at a time.

Previous

Two Sum

Next

Search in Rotated Sorted Array

All problems
Keep practicing

Related problems

Practice similar problems to strengthen your understanding of the underlying algorithm and improve pattern recognition.

Easy

Two Sum

ArrayHashMap
Solve now
Hard

Longest Substring Without Repeating Characters

StringHashMap
Solve now