Solved Valid Anagram problem using hash maps in NeetCode150

Day 8 of #100DaysOfNeetCode | #NeetCode150 Today I solved the Valid Anagram problem — a fundamental string question that focuses on frequency counting and efficient comparison. 1. Valid Anagram -> Goal: Determine if two strings are anagrams, meaning one string can be formed by rearranging the letters of the other. -> Brute Force: Sort both strings and compare them directly. Simple but has a time complexity of O(n log n+ m long m). -> Optimized: Use two hash maps to count the frequency of each character in both strings, then compare their counts. If every character frequency matches, the strings are anagrams. Key idea: Counting frequencies eliminates the need for sorting, reducing time complexity. Example: s = "racecar", t = "carrace" → Output: true s = "hello", t = "bello" → Output: false Time Complexity: O(n), Space: O(1) (constant alphabet size) Takeaway: Hash maps are a powerful tool for counting and comparing elements efficiently, a concept that extends to many string and array problems. #Day8 #JavaScript #LeetCode #NeetCode150 #DSA #ProblemSolving #100DaysOfNeetCode #CodingJourney

  • text

To view or add a comment, sign in

Explore content categories