Valid Anagram Solution in Java

🚀 Day 62/365 — DSA Challenge Today's problem was simple in statement... but powerful in concept. Solved: 242. Valid Anagram The task: Given two strings s and t, return true if t is an anagram of s. An anagram means: Same characters. Same frequency. Different order allowed. 💡 My Approach Instead of sorting both strings, I used a frequency counter array of size 26. Steps: • If lengths differ -> return false • Increment count for each character in s • Decrement count for each character in t • If all values return to zero -> it's an anagram ⚡ Why this approach? Sorting -> O(n log n) Frequency count -> O(n) Time Complexity: O(n) Space Complexity: O(1) (fixed 26 letters) What I learned today Sometimes the optimal solution is just about counting correctly. Not every string problem needs sorting. Clean. Efficient. Intentional. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #Consistency #ProblemSolving

  • text

To view or add a comment, sign in

Explore content categories