Перейти к содержимому

Valid Anagram in Java | Sorting vs Frequency Array vs HashMap | LeetCode 242

Code With Ankit

0:00 / 0:00

Valid Anagram in Java | Sorting vs Frequency Array vs HashMap | LeetCode 242

176 просмотров · 3 дня назад
Code With Ankit
30 подписчиков
176 просмотров · 3 дня назад
Valid Anagram (LeetCode 242) in Java | Sorting vs Array vs HashMap In this video, I explain *LeetCode 242: Valid Anagram* step by step in Java using three approaches — **Sorting, Frequency Array, and HashMap**. We start with the Sorting approach, optimize it to an *O(n) Frequency Array solution**, and finally discuss an important interview follow-up: **What if the input contains Unicode characters?* The problem gives us two strings `s` and `t`, and we need to determine whether `t` is an anagram of `s`. For example: Input: `s = "anagram"` `t = "nagaram"` Output: `true` Both strings contain the same characters with the same frequencies, even though their ordering is different. 🔥 WHAT YOU'LL LEARN ✅ What is a Valid Anagram? ✅ Sorting Approach — O(n log n) ✅ Frequency Array Approach — O(n) ✅ How `character - 'a'` maps letters to indices ✅ HashMap approach for a broader character set / Unicode follow-up ✅ Time & Space Complexity of each approach ✅ Java implementation and interview approach ⏱️ CHAPTERS 00:00 Problem Statement & Introduction 00:20 What is an Anagram? 01:02 Understanding Examples 02:04 Sorting Approach 02:46 Sorting – Time & Space Complexity 03:24 Sorting Solution in LeetCode Editor 04:57 Can We Optimize the Solution? 05:25 Frequency Array Approach 06:10 Frequency Array Dry Run 08:10 Optimized Java Solution 09:47 Optimized Solution in LeetCode Editor 11:36 Follow-up: What if Input Contains Unicode Characters? 12:12 HashMap Approach 14:47 HashMap & Frequency Array – Time & Space Complexity Problem: LeetCode 242 – Valid Anagram Difficulty: Easy Topics: String, Hash Table, Sorting Language: Java Series: Blind 75 🔑 KEY IDEA Two strings are anagrams if they contain the **same characters with the same frequencies**. The ordering of those characters can be different. For lowercase English letters, we can use a fixed frequency array of size 26: `int[] freq = new int[26];` Characters can be mapped to array indices using: `character - 'a'` For example: `'a' - 'a' = 0` `'b' - 'a' = 1` `'n' - 'a' = 13` `'z' - 'a' = 25` This lets us compare character frequencies in linear time without sorting. 📊 COMPLEXITY COMPARISON Sorting Approach: Time Complexity: *O(n log n)* Extra Space: *O(n)* when creating character arrays Frequency Array Approach: Time Complexity: *O(n)* Extra Space: *O(1)* for a fixed 26-character alphabet HashMap Approach: Average Time Complexity: *O(n)* Space Complexity: *O(n)* in the general case 💡 INTERVIEW FOLLOW-UP *What if the input contains Unicode characters?* The fixed array of size 26 works when the input is restricted to lowercase English letters. For a broader character set, we can use a *HashMap-based frequency counting approach* instead. For full Unicode handling in Java, Unicode code points should be considered rather than assuming every character fits into a single `char`. 🔗 Problem: https://leetcode.com/problems/valid-a... Try solving the problem yourself before watching the complete optimized solution. This problem demonstrates an important interview pattern: start with a straightforward **O(n log n) Sorting solution**, recognize that only character frequencies matter, and optimize it to an **O(n) solution**. If this video helped you, LIKE the video and SUBSCRIBE for more **LeetCode solutions, Blind 75 problems, DSA in Java, algorithms, and coding interview preparation**. 💬 Comment below: *Which approach would you prefer in an interview — Sorting, Frequency Array, or HashMap?* Keep Learning, Keep Growing 🚀 #LeetCode242 #ValidAnagram #Java #Blind75 #DSA #HashMap #Sorting #CodingInterview #LeetCodeSolution #StringProblems