🚀 Day 547 of #750DaysOfCode 🚀 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 🚀 Day 547 of #750DaysOfCode 🔥 Solved: Check if Strings Can be Made Equal With Operations II (LeetCode Medium) 💡 Problem Insight We can swap characters at indices i and j such that: 👉 (j - i) is even 🧠 Key Observation If (j - i) is even ⇒ 👉 indices belong to the same parity group So: Even indices form one group Odd indices form another group 👉 We can rearrange freely within each group 🚫 But cannot mix between them ⚡ Optimized Approach (Single Array Trick) Instead of using two arrays, we can: Use a single frequency array of size 52 First 26 → even indices Next 26 → odd indices 👉 Clever use of: int off = (i & 1) * 26; i & 1 = 0 → even → offset 0 i & 1 = 1 → odd → offset 26 📈 Complexity Time: O(n) Space: O(1) 💬 Key Takeaway This problem is a great example of: 👉 Index grouping + frequency balancing And the optimization shows: 👉 How bit manipulation (i & 1) + offset trick can reduce space & simplify logic 🔥 🔁 Small optimizations → Big impact over time Consistency continues 💯 #LeetCode #Algorithms #DataStructures #Java #ProblemSolving #CodingChallenge #750DaysOfCode #Consistency
LeetCode Solution: Check if Strings Can be Made Equal With Operations II
More Relevant Posts
-
🚀 100 Days of Code Day-25 LeetCode Problem Solved: Reverse Nodes in k-Group I recently worked on a Linked List problem that focuses on reversing nodes in groups of size k while preserving the remaining structure if the group size is less than k. 🔹 Strengthened my understanding of pointer manipulation 🔹 Improved problem decomposition skills 🔹 Practiced recursive thinking for efficient implementation 💡 Key takeaway: Breaking complex problems into smaller, manageable parts significantly simplifies the solution approach. Continuing to build consistency in problem-solving and deepen my understanding of Data Structures & Algorithms. #LeetCode #DataStructures #Algorithms #Java #ProblemSolving #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 23 of my #30DayCodeChallenge: The Art of Pruning! The Problem: Permutations II. Generating all unique permutations of a collection that contains duplicates. The challenge isn't just finding the arrangements, but ensuring we don't repeat work or results. The Logic: This problem is a deep dive into Backtracking with a strategic Pruning layer: 1. The Frequency/State Tracking: Since we have duplicate numbers, we can't just rely on the values themselves. I used a vis [] (visited) boolean array to keep track of which specific index in the array is currently being used in our recursion tree. 2. Sorting for Symmetry Breaking: Before starting the recursion, sorting the array is the secret sauce. By grouping identical numbers together, we can easily identify when we are about to make a "dur"-ate choice." 3. Backtracking: Standard push, recurse, and pop. We explore every valid path, then "undo" our choice by setting vis [j] = false to backtrack and try the next possibility. One step closer to mastery. The logic is getting sharper! Onward to Day 24! #Java #Algorithms #DataStructures #Backtracking #LeetCode #150DaysOfCode #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 76 — Slow & Fast Pointer (Find the Duplicate Number) Continuing the cycle detection pattern — today I applied slow‑fast pointers to an array problem where the values act as pointers to indices. 📌 Problem Solved: - LeetCode 287 – Find the Duplicate Number 🧠 Key Learnings: 1️⃣ The Problem Twist Given an array of length `n+1` containing integers from `1` to `n` (inclusive), with one duplicate. We must find the duplicate without modifying the array and using only O(1) extra space. 2️⃣ Why Slow‑Fast Pointer Works Here - Treat the array as a linked list where `i` points to `nums[i]`. - Because there’s a duplicate, two different indices point to the same value → a cycle exists in this implicit linked list. - The duplicate number is exactly the entry point of the cycle (same logic as LeetCode 142). 3️⃣ The Algorithm in Steps - Phase 1 (detect cycle): `slow = nums[slow]`, `fast = nums[nums[fast]]`. Wait for them to meet. - Phase 2 (find cycle start): Reset `slow = 0`, then move both one step at a time until they meet again. The meeting point is the duplicate. 4️⃣ Why Not Use Sorting or Hashing? - Sorting modifies the array (not allowed). - Hashing uses O(n) space (not allowed). - Slow‑fast pointer runs in O(n) time and O(1) space — perfect for the constraints. 💡 Takeaway: This problem beautifully demonstrates how the slow‑fast pattern transcends linked lists. Any structure where you can define a “next” function (here: `next(i) = nums[i]`) can be analyzed for cycles. Recognizing this abstraction is a superpower. No guilt about past breaks — just another pattern mastered, one day at a time. #DSA #SlowFastPointer #CycleDetection #FindDuplicateNumber #LeetCode #CodingJourney #Revision #Java #ProblemSolving #Consistency #GrowthMindset #TechCommunity #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 565 of #750DaysOfCode 🚀 🔍 Problem Solved: Minimum Absolute Distance Between Mirror Pairs Today’s problem was all about identifying mirror pairs in an array — where reversing one number equals another — and finding the minimum index distance between such pairs. 💡 Key Insight: Instead of brute force (O(n²)), we can optimize using a HashMap to track previously seen reversed values. 🧠 Approach: Traverse the array once For each number: Check if it already exists in the map → update minimum distance Compute its reverse Store the reversed number with its index Return the minimum distance, or -1 if no pair exists 📊 Complexity: Time: O(n × d) → d = number of digits Space: O(n) 🔥 Takeaway: Using a reverse transformation + hashmap lookup converts a nested loop problem into a linear scan — a classic optimization pattern! #Day565 #750DaysOfCode #LeetCode #Java #DataStructures #Algorithms #CodingJourney #HashMap #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 546 of #750DaysOfCode🚀 🔥 Solved: Check if Strings Can be Made Equal With Operations I (LeetCode Easy) 💡 Problem Insight We are allowed to swap characters at indices where: 👉 j - i = 2 This means: Index 0 ↔ 2 (even positions) Index 1 ↔ 3 (odd positions) 🚫 But we cannot mix even and odd indices 🧠 Key Observation The string is divided into 2 independent groups: Even indices → (0, 2) Odd indices → (1, 3) 👉 We can rearrange within each group freely 👉 So both groups must match between s1 and s2 ⚡ Approach Extract characters: Even indices from both strings Odd indices from both strings Sort both groups Compare: Even parts must match Odd parts must match 📈 Complexity Time: O(1) Space: O(1) 💬 Key Takeaway Sometimes problems look like string manipulation, but the real trick is: 👉 Understanding constraints → grouping → independent transformations 🔁 Consistency check ✔️ Another day, another step forward 🚀 #LeetCode #DataStructures #Algorithms #Java #CodingChallenge #ProblemSolving #100DaysOfCode #Consistency
To view or add a comment, sign in
-
-
🚀 Day 34 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Maximum Product of Two Digits Problem Insight: Given a number, find the maximum product of any two digits in it. Approach: • Traverse the number digit by digit • Keep track of the two largest digits (large1 and large2) • Multiply the two largest digits to get the answer • No extra data structures required, simple comparison logic Time Complexity: • O(log n) — proportional to the number of digits Space Complexity: • O(1) — only a few variables used Key Learnings: • Simple comparison logic can replace sorting or extra arrays • Breaking down the number digit by digit is often sufficient • Edge cases like single-digit numbers should be considered Takeaway: Finding the largest values while iterating can simplify problems and reduce complexity. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #MathTricks
To view or add a comment, sign in
-
-
#100DaysOfCode | Day 2 of my LeetCode challenge. Today’s problem: 1365. How Many Numbers Are Smaller Than the Current Number. While the problem seems simple, it’s a perfect example of how choosing the right data structure can drastically change performance. Here is how I broke it down: 1. The Brute Force Approach The most simple and easy way is to use nested loops to compare every number with every other number. Logic: For each element, loop through the entire array and count smaller values. Time Complexity: O(N^2) Space Complexity: O(N) (to store the result) 2. The Sorting + HashMap Approach A more scalable way is to sort the numbers. In a sorted array, a number's index is equal to the count of numbers smaller than it. Logic: Clone the array, sort it, and store the first occurrence of each number in a HashMap. Time Complexity: O(N log N) (due to sorting) Space Complexity: O(N) (to store the map) Use - Works for any range of numbers (including very large or negative ones). 3. The Frequency Array (Counting Sort Logic) Since the problem constraints were small (0 to 100), this is the most optimized solution. Logic: Count the frequency of each number using an array of size 101, then calculate a running prefix sum. Time Complexity: O(N) (Linear time) Space Complexity: O(1) (The frequency array size is constant) #LeetCode #100DaysOfCode #Java #SoftwareEngineering #DataStructures #Algorithms
To view or add a comment, sign in
-
Day 36/50 🚀 Solved “Valid Parentheses” today — a classic stack problem, but a great reminder that simple concepts can be powerful when applied correctly. 💡 Key takeaway: Using a stack makes it easy to track opening brackets and validate matching pairs efficiently. Every closing bracket should correspond to the most recent unmatched opening one — LIFO in action. ⚙️ What I focused on: Clean conditional checks Avoiding unnecessary complexity Writing readable, structured code 📈 Result: Accepted ✅ Optimized runtime & solid performance On to Day 37 🔥 #50DaysOfCode #DataStructures #Algorithms #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Just solved the Contains Duplicate problem with a fresh perspective! Instead of going with the traditional sorting + two pointer approach, I used the property of Set (uniqueness) to achieve an O(n) time complexity solution. 💡 Approach: - Traverse the array once - Use a HashSet to track elements - If an element already exists → duplicate found ⚡ Time Complexity: O(n) 📦 Space Complexity: O(n) 🔁 On the other hand, the sorting + two pointer approach gives: - Time: O(n log n) - Space: O(1) 👉 So it’s a classic trade-off: - Optimize time → use Set - Optimize space → use Sorting+two pointer Really enjoyed breaking down this problem and comparing approaches — small problems like this build strong intuition for bigger ones 💪 #DataStructures #Algorithms #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
Day 09 of #50DaysOfLeetCode Challenge Just tackled the "Combination Sum" problem! This was a fantastic exercise in backtracking. The challenge is to find all unique combinations of numbers that sum up to a specific target, with the twist that you can use the same number multiple times. Key Insights: Backtracking Power: It’s all about exploring every possible path and "backtracking" as soon as the sum exceeds the target. State Space Tree: Visualizing how the recursion branches out helped me understand how to avoid duplicate combinations while allowing multiple uses of the same element. Decision Making: Learning when to include an element and when to move to the next index is crucial for optimizing the search. Each day, the logic gets sharper and the problems get more interesting! #DataStructures #Algorithms #CodingJourney #Java #Backtracking #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
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