🔥 DSA Challenge – Day 128/360 🚀 📌 Topic: Recursion 🧩 Problem: Letter Combinations of a Phone Number 💡 Problem Statement: Given a string of digits (2–9), return all possible letter combinations that the number could represent based on the classic phone keypad mapping. 🔍 Example: Input: "23" Output: ["ad","ae","af","bd","be","bf","cd","ce","cf"] 💡 Approach: Backtracking (Recursion) We treat this like a decision tree: Each digit maps to multiple characters For every digit, we try all possible letters Build combinations step-by-step using recursion 👉 Key Idea: Choose → Explore → Backtrack Use StringBuilder for efficient string manipulation ⚙️ Steps: Create a mapping for digits → letters Start recursion from index 0 For each digit: Loop through its mapped letters Add letter to current string Move to next digit Backtrack (remove last character) 🚀 Code Highlights: ✔️ Base case: when index reaches end → add combination ✔️ Efficient backtracking using StringBuilder ✔️ Avoids extra space by modifying same string ⏱️ Time Complexity: O(4^N) 📦 Space Complexity: O(N) (recursion stack) Master this → You master recursion 🔥 #DSA #Java #Backtracking #CodingInterview #LeetCode #128DaysOfCode #Programming #Tech #ProblemSolving
Dileep Kumar Maurya’s Post
More Relevant Posts
-
🔥 Day 364 – Daily DSA Challenge! 🔥 Problem: 🎯 Find K Closest Elements Given a sorted array arr, an integer k, and a target x, return the k closest elements to x in ascending order. 💡 Key Insight — Binary Search on Window Instead of checking each element, we binary search the starting index of a window of size k. Search space: Possible windows → [0 ... n - k] 🧠 Decision Logic At index mid, compare: Distance of left boundary → x - arr[mid] Distance of right boundary → arr[mid + k] - x We choose direction based on: ⚡ Algorithm Steps ✅ Initialize: left = 0, right = n - k ✅ Binary search: If left side is farther → shift right Else → move left ✅ Final window starts at left ✅ Collect k elements ⚙️ Complexity ✅ Time Complexity: O(log(n - k) + k) (binary search + result building) ✅ Space Complexity: O(k) 💬 Challenge for you 1️⃣ Why do we search on window positions instead of elements? 2️⃣ Can you solve this using a heap approach? 3️⃣ What if array was unsorted? #DSA #Day364 #LeetCode #BinarySearch #SlidingWindow #Arrays #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
🚀 Day 45/60 – DSA Challenge Today’s problem was about searching in a Rotated Sorted Array using Binary Search 🔍 🔍 Problem Solved: Given a rotated sorted array, efficiently find the index of a target element. 🧠 Approach I Used: Instead of directly applying binary search, I broke the problem into two steps: 1️⃣ Find the pivot (rotation point) using binary search 2️⃣ Apply binary search on the correct half of the array Left half → sorted from start to pivot-1 Right half → sorted from pivot to end ⚡ Key Insight: By identifying the pivot, the problem becomes two simple binary searches Careful boundary checks (like target >= nums[0]) ensure we search in the correct half Handling edge cases (like pivot at index 0) is crucial for correctness 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Complex problems can often be simplified by breaking them into smaller parts Binary Search is not just about searching—it’s about understanding structure Boundary conditions can make or break your solution Debugging edge cases today made the concept even clearer 💡 #Day45 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
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
-
-
🔥 DSA Challenge – Day 127/360 🚀 📌 Topic: Backtracking 🧩 Problem: Subsets II Problem Statement: Given an integer array that may contain duplicates, return all possible subsets such that the solution set does not contain duplicate subsets. 🔍 Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] 💡 Approach: Backtracking + Sorting 1️⃣ Step 1 – Sort the array to group duplicate elements together 2️⃣ Step 2 – Use recursion to generate all subsets 3️⃣ Step 3 – Skip duplicate elements using condition (i != ind && nums[i] == nums[i-1]) ⏱ Complexity: Time: O(2^n) Space: O(n) (recursion stack + subset storage) 📚 Key Learning: Sorting + smart skipping of duplicates helps avoid repeated subsets in backtracking problems. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #360DaysOfCode #LeetCode #Backtracking
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
-
-
🔥 DSA Challenge – Day 125/360 🚀 📌 Topic: Recursion 🧩 Problem: Combination Sum Problem Statement: Given an array of distinct integers and a target, return all unique combinations where numbers sum up to the target. Same element can be used multiple times. 🔍 Example: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] 💡 Approach: Backtracking 1️⃣ Step 1 – Start from index 0 and try picking each element 2️⃣ Step 2 – If element ≤ target, include it and reduce target 3️⃣ Step 3 – Backtrack (remove element) and move to next index ✔ Use recursion to explore all possibilities ✔ Reuse same element (stay on same index) ✔ Stop when target becomes 0 (valid answer) ✔ Skip when index reaches end ⏱ Complexity: Time: O(2^n * k) (k = avg length of combination) Space: O(k * x) (x = number of combinations) 📚 Key Learning: Backtracking is all about making choices, exploring, and undoing them efficiently. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #360DaysOfCode #LeetCode #Backtracking 🚀
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟓 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem extended the previous one by introducing duplicates in a rotated sorted array. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Minimum in Rotated Sorted Array II 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used modified binary search • Compared nums[mid] with nums[right] Logic: • If nums[mid] > nums[right] → minimum is in right half • If nums[mid] < nums[right] → minimum is in left half (including mid) • If equal → cannot decide, so shrink search space (right--) 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Duplicates can break standard binary search logic • When unsure, safely reduce the search space • Edge cases often define the complexity of the problem • Small changes in conditions can affect performance 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Average Time: O(log n) • Worst Case: O(n) (due to duplicates) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 When the data becomes ambiguous, focus on reducing the problem safely instead of forcing a decision. 55 days consistent 🚀 On to Day 56. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🔥 Day 36 of #75DaysofLeetCode 🔥 LeetCode 437 – Path Sum III | Prefix Sum + DFS Magic! Struggled with counting paths in a binary tree that sum up to a target? 🤔 This problem looks like a typical tree traversal… but the optimal solution is 🔥 next-level thinking. 💡 Problem Insight: We need to count all paths (not necessarily from root) where the sum equals targetSum. Condition → Path must go downwards (parent → child). 🚫 Brute Force Idea: Start DFS from every node → check all paths ⏱️ Time Complexity: O(n²) (not efficient) ⚡ Optimized Approach: Prefix Sum + HashMap Instead of recomputing sums, we: ✔ Maintain a running sum (currentSum) ✔ Store previous sums in a HashMap ✔ Check if (currentSum - targetSum) exists 👉 This tells us how many valid paths end at the current node! 🧠 Core Logic: Add current node value to currentSum Check: currentSum - targetSum in map Update map before recursion Backtrack after recursion ⏱️ Complexity: Time: O(n) Space: O(n) 🚀 Key Takeaway: Whenever you see: 👉 "subarray sum = k" or 👉 "path sum = target" Think Prefix Sum + HashMap 💡 #LeetCode #DSA #Java #CodingInterview #BinaryTree #Programming #SoftwareEngineering #100DaysOfCode
To view or add a comment, sign in
-
-
Day 47 of Daily DSA 🚀 Solved LeetCode 74: Search a 2D Matrix ✅ Problem: Given a sorted 2D matrix where: • Each row is sorted • First element of each row > last element of previous row Find whether a target exists in the matrix. Approach: Used an optimized staircase search (top-right traversal). Steps: Start from top-right corner If element == target → return true If element > target → move left If element < target → move down Continue until found or out of bounds ⏱ Complexity: • Time: O(n + m) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.84 MB Sometimes choosing the right starting point (top-right) makes the search super efficient 💡 #DSA #LeetCode #Java #Matrix #BinarySearch #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🔥 Day 140/360 – This Trick Generates All Subsets Instantly 🚀 Most people solve this using recursion… But today I learned how to generate all subsets using Bit Manipulation👇 📌 Topic: Bit Manipulation + Array 🧩 Problem: Subsets (Power Set) 📝 Problem Statement: Given an integer array, return all possible subsets (the power set). 🔍 Example: Input: [1, 2, 3] Output: [[], [1], [2], [1,2], [3], [1,3], [2,3], [1,2,3]] 💡 Approach: Bit Masking (Optimized) ✔ Step 1 – Calculate total subsets = 2ⁿ using (1 << n) ✔ Step 2 – Loop from 0 to 2ⁿ - 1 (each number represents a subset) ✔ Step 3 – Use bits to decide whether to include an element ⚡ Key Idea: Each number (mask) represents a subset in binary form. If a bit is ON → include that element. ⏱ Complexity: Time → O(n × 2ⁿ) Space → O(n × 2ⁿ) 📚 What I Learned: Bit manipulation can replace recursion in subset generation and makes the logic easier to visualize in binary. 🚀 Why This Matters: This concept is widely used in backtracking, DP, and interview problems. #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #BitManipulation #TechJourney #140DaysOfCode
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