Valid Anagram Problem Solution in Java

Day 23 of problem solving– Valid Anagram | DSA in Java Today I solved the “Valid Anagram” problem. Problem Statement: Given two strings s and t, return true if t is an anagram of s, and false otherwise. 🔹 An anagram means both strings contain the same characters with the same frequency. Approach I Used: Instead of sorting both strings (which takes O(n log n)), I used a frequency array technique for better performance. Logic: If lengths are different → return false. Create an integer array of size 26. Increase count for characters in first string. Decrease count for characters in second string. If all values are zero → it’s a valid anagram. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) (Fixed size array) 📌 Key Learning: Using a frequency counter is more efficient than sorting when dealing with character-based problems. Consistency > Motivation Slowly building problem-solving strength every day. #DSA #Java #100DaysOfCode #CodingJourney #SoftwareEngineer

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories