🚀 LeetCode — Problem 242 | Day 14 💡 Problem: Valid Anagram --- 🧠 Problem: Given two strings s and t, return true if t is an anagram of s, otherwise false. --- 🧠 Approach: - First check length: • If lengths differ → not an anagram - Use a frequency array of size 26 - Traverse both strings: • Increment count for s • Decrement count for t - Finally check: • If all values are 0 → valid anagram --- ⚙️ Core Logic: - freq[s.charAt(i) - 'a']++ - freq[t.charAt(i) - 'a']-- 👉 If all counts become zero → both strings have same characters --- ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) (fixed size array) --- ⚠️ Edge Cases: - Different lengths → false - Same characters, different order → true - Completely different strings → false --- 🔍 Insight: Instead of sorting, count character frequency --- 🔑 Key Learning: - Frequency counting is faster than sorting - Fixed-size array gives O(1) space - Simple and optimal approach for character problems --- "Compare frequency of characters using a fixed array instead of sorting." --- #LeetCode #DSA #Java #Strings #CodingJourney
Valid Anagram Problem Solution
More Relevant Posts
-
📘 DSA Journey — Day 29 Today’s focus: Binary Search on answer space. Problem solved: • Find Peak Element (LeetCode 162) Concepts used: • Binary Search • Observing slope / trend • Search space reduction Key takeaway: The goal is to find a peak element (an element greater than its neighbors). Instead of checking all elements, we use binary search by observing the slope: • If nums[mid] < nums[mid + 1] → we are on an increasing slope, so a peak must exist on the right side • Else → we are on a decreasing slope, so a peak lies on the left side (including mid) By following this logic, we eliminate half of the search space each time and find a peak in O(log n) time. The key insight is: A peak is guaranteed to exist, and the direction of slope helps guide the search. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 33 Today’s focus: Binary Search for next greater element. Problem solved: • Find Smallest Letter Greater Than Target (LeetCode 744) Concepts used: • Binary Search • Upper bound concept • Circular handling Key takeaway: The goal is to find the smallest character strictly greater than a given target in a sorted array. This is a classic upper bound problem: We use binary search to find the first element greater than the target. During search: • If letters[mid] > target, store it as a possible answer and move left • Else move right An important edge case: If no character is greater than the target, we return the first element (circular behavior). Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 13/100 – LeetCode Journey Today’s problem: Squares of a Sorted Array 🔥 Approach 1 (Brute Force + Sorting) 👉 Workflow: Square every element Store in new array Sort the array ⚡ Time Complexity: O(n log n) (because of sorting) 💡 Approach 2 (Two Pointer – Optimized) 👉 Workflow: Use two pointers (left, right) Compare squares of both ends Place larger square at the end of result array Move pointers accordingly ⚡ Time Complexity: O(n) 🧠 Why Optimized is Better? Original array is already sorted But after squaring, order may break (because negatives become positive) 👉 Example: [-4, -1, 0, 3, 10] Squares → [16, 1, 0, 9, 100] ❌ not sorted Sorting again → O(n log n) Two-pointer uses property of sorted array → O(n) 👉 We compare largest absolute values from ends instead of sorting 🧠 Key Insight: Largest square always comes from either: leftmost (large negative) or rightmost (large positive) 🧠 Space Complexity: O(n) (result array) Learning optimization step by step 🚀 #100DaysOfCode #LeetCode #DSA #Java
To view or add a comment, sign in
-
-
🚀 Day 23/100 — #100DaysOfLeetCode Another challenging day — pushing deeper into advanced Sliding Window problems 💻🔥 ✅ Problem Solved: 🔹 LeetCode 76 — Minimum Window Substring 💡 Concepts Used: Sliding Window HashMap Frequency Counting Two Pointer Optimization 🧠 Key Learning: The goal was to find the smallest substring containing all characters of another string. Key approach: Expand the window until all required characters are included. Track character frequencies carefully. Shrink the window while maintaining validity to get the minimum length. ⚡ Big Insight: Sliding Window isn’t only about longest substrings — it can also efficiently solve minimum window optimization problems. This problem really tested attention to detail and window management logic. Consistency is turning difficult problems into enjoyable challenges 🚀 #100DaysOfLeetCode #LeetCode #DSA #SlidingWindow #Algorithms #Java #ProblemSolving #CodingJourney #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 49 🔍 Solved: Largest Number Today I worked on an interesting problem where the goal was to arrange numbers such that they form the largest possible number. 💡 Key Insight: Instead of normal sorting, we compare numbers based on their string combinations (like "ab" vs "ba"). 📌 Approach: ✔ Converted integers to strings ✔ Used a custom comparator for sorting ✔ Compared (a + b) and (b + a) ✔ Sorted in descending order based on best combination ✔ Handled edge case when all elements are 0 Why this works: By comparing two numbers in different orders, we ensure that the arrangement always produces the maximum possible value when concatenated. 🎯 What I Learned: This problem taught me that sorting can go beyond numbers—custom logic and string manipulation are powerful tools in problem solving. #Java #DSA #LeetCode #Sorting #Comparator #CodingJourney #ProblemSolving #TechSkills 🚀
To view or add a comment, sign in
-
-
🚀 Day 59 of My Consistency Journey Today, I solved String Compression (LeetCode 443) — and honestly, it taught me a powerful lesson about thinking beyond brute force. 🔍 Key Learning: At first, I was tempted to use a HashMap to count frequencies, but that approach fails because this problem is about consecutive characters, not total frequency. 💡 Core Concept: 👉 Use Two Pointers (i & j) i → reads and counts characters j → writes compressed result 📌 Main Idea: "Group → Count → Write" Example: Input: a a a b c c Output: a3 b c2 ⚡ Important Insight: Handling counts like 12 or 15 means writing digits separately ('1', '2'), not as a single number. 🧠 What I Improved Today: Understanding in-place array modification Mastering two-pointer technique Writing optimized and clean logic Consistency is slowly turning confusion into clarity 💯 #Day59 #100DaysOfCode #Java #DSA #LeetCode #CodingJourney #PlacementPreparation #KeepLearning
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
✅ Solved LeetCode 217 — Contains Duplicate! Given an integer array nums, return true if any value appears at least twice. 🧠 My Approach: Sort + Linear Scan → Sort the array → adjacent duplicates are guaranteed to be neighbors → One pass to check if nums[i] == nums[i-1] → Time: O(n log n) | Space: O(1) 💡 Key Insight: Sorting brings duplicates side by side — no need for extra space like a HashSet. Trade time for space! ```java Arrays.sort(nums); for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) return true; } return false; ``` 🔄 Alternative approaches: • HashSet → O(n) time, O(n) space • Brute force → O(n²) time (avoid!) Every problem teaches you a new trade-off. Keep grinding! 💪 #LeetCode #DSA #CodingInterview #Java #ProblemSolving #SoftwareEngineering #100DaysOfCode #Programming
To view or add a comment, sign in
-
-
🚀 Day 571 of #750DaysOfCode 🚀 🔍 Problem Solved: Sum of Distances Today’s problem looked like a classic brute-force trap 👀 At first glance, comparing every pair gives an O(n²) solution — but with constraints up to 10⁵, that’s not going to work. 💡 Key Insight: Instead of comparing all pairs, we can: 👉 Group indices of the same value 👉 Use prefix sums to efficiently calculate distances 🧠 Approach: Group indices by value (using HashMap) For each group: Build prefix sum of indices For each index: Left contribution → i * count - sum Right contribution → sum - i * count Combine both to get final result 📈 Complexity: Time: O(n) Space: O(n) ✨ Takeaway: When you see distance-based problems: 👉 Think in terms of contributions instead of pair comparisons 👉 Prefix sums can turn expensive computations into linear time Another strong pattern added to the toolkit 💪 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #PrefixSum #Algorithms #LearningEveryday
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 16/50 💡 Approach: Dynamic Programming (Bottom-Up) A pure recursion approach explodes to O(2ⁿ) — exponential! By storing subproblem results in a DP array, we decode the entire string in a single linear pass. 🔍 Key Insight: → dp[i] = number of ways to decode first i characters → Single digit (1-9): dp[i] += dp[i-1] → Two digits (10-26): dp[i] += dp[i-2] → '0' alone is always invalid — handle carefully! → Build up the answer from base cases 📈 Complexity: ❌ Recursion (no memo) → O(2ⁿ) Time ✅ Dynamic Programming → O(n) Time, O(n) Space 🚀 Optimized DP → O(n) Time, O(1) Space (only 2 variables needed!) DP is not just an algorithm — it's a mindset. Break the problem, store the result, build the solution! 🧩 #LeetCode #DSA #DynamicProgramming #Java #ADA #PBL2 #LeetCodeChallenge #Day16of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #DecodeWays
To view or add a comment, sign in
-
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development