#200DaysOfCode – Day 103 Generate Parentheses Problem :- Generate Parentheses Task :- Given n pairs of parentheses, generate all combinations of well-formed parentheses. Example: Input: n = 3 Output: ["((()))","(()())","(())()","()(())","()()()"] My Approach: Used Backtracking to build the string step by step. Tracked the count of open and close brackets. Added '(' only when open < n. Added ')' only when close < open. Stored the result when the string length reached 2 * n. Time Complexity: Exponential (Catalan-based) Space Complexity: O(n) (recursion stack) Key Takeaway: Instead of generating all possibilities and checking validity later, applying constraints during recursion leads to clean, efficient, and elegant solutions. #takeUforward #200DaysOfCode #Java #LeetCode #ProblemSolving #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie #CleanCode
200 Days of Code: Generate Parentheses with Backtracking
More Relevant Posts
-
#200DaysOfCode – Day 106 Backtracking & Recursion | Combination Sum II Problem: Combination Sum II Task: Given an array of candidate numbers and a target, find all unique combinations where the numbers sum up to the target. Each number can be used only once, and duplicate combinations are not allowed. Example: Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [1,1,6], [1,2,5], [1,7], [2,6] My Approach: Sorted the array to handle duplicates efficiently. Used backtracking to explore all valid combinations. Skipped duplicate elements to avoid repeated combinations. Pruned recursion when the current value exceeded the target. Time Complexity: Exponential (Backtracking) Space Complexity: O(N) due to recursion stack Backtracking becomes much cleaner and more efficient when combined with sorting and smart pruning. Understanding when to skip elements is just as important as choosing them. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie #Consistency
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 105 Backtracking & Recursion 🔹 Problem:- Combination Sum (LeetCode 39) Task: Given an array of distinct integers and a target value, find all unique combinations where the chosen numbers sum to the target. Each number can be used multiple times. Example: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3], [7]] My Approach: Used Backtracking to explore all possible combinations. At each step, decided to pick or skip the current element. Allowed reuse of elements by staying on the same index. Backtracked once the target became negative or a valid combination was found. Time Complexity: Exponential (based on number of combinations) Space Complexity: O(Target) (recursion stack + current path) Key Takeaway: Backtracking is all about choices and reversals. Once the pick / not-pick pattern becomes clear, complex problems start feeling much simpler. #takeUforward #200DaysOfCode #Java #Backtracking #Recursion #ProblemSolving #LeetCode #DSA #CodeNewbie #LearningInPublic #Consistency
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 107 Subsets with Duplicates Problem:- Subsets II (LeetCode 90) Task: Given an integer array nums that may contain duplicates, return all possible unique subsets (power set). Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] My Approach: Sorted the array to bring duplicate elements together. Used Backtracking to generate all subsets. Skipped duplicate elements at the same recursion level to avoid repeating subsets. Complexity Analysis: Time Complexity: O(2^N) Space Complexity: O(N) (recursion stack, excluding output) Handling duplicates becomes much easier when the input is sorted and recursion levels are controlled carefully. Backtracking problems look complex at first but the logic becomes clean once broken into steps. #takeUforward #100DaysOfCode #Java #LeetCode #ProblemSolving #Backtracking #Recursion #DSA #CodingJourney #CleanCode #DeveloperLife
To view or add a comment, sign in
-
-
#200DaysOfCode – Day 108 Combination Sum III Problem:- Combination Sum III Task:- Find all valid combinations of k numbers that sum up to n, using only numbers from 1 to 9, where: Each number is used at most once No duplicate combinations are allowed Example: Input: k = 3, n = 7 Output: [[1, 2, 4]] My Approach:- Used Backtracking (DFS) to explore all possible combinations. Started from a given number to avoid duplicates. Stopped recursion when: The combination size exceeded k The sum became negative Added the combination to the result only when: Exactly k numbers were chosen The sum became 0 Time Complexity:- Exponential (bounded due to range 1–9) Space Complexity:- O(k) (recursive stack + temporary list) Backtracking may look complex at first, but with clear base conditions and pruning, it becomes a powerful and elegant tool for solving combination problems efficiently. #takeUforward #200DaysOfCode #Java #ProblemSolving #LeetCode #Backtracking #Recursion #DSA #CodingJourney #CodeNewbie
To view or add a comment, sign in
-
-
🔹 Day 98 – LeetCode Practice 📌 Problem: Path Sum II (LeetCode #113) 📊 Difficulty: Medium 🧠 Problem Overview: Given the root of a binary tree and a target sum, the task is to find all root-to-leaf paths where the sum of node values equals the target. Each valid path should be returned as a list of values. ✅ Approach Used: Traversed the tree using depth-first search. Maintained the current path and cumulative sum while moving down the tree. When a leaf node was reached, checked if the sum matched the target. Used backtracking to explore all possible paths correctly. 📈 Submission Results: Status: Accepted ✅ Runtime: 2 ms Memory Usage: 45.54 MB 💡 Reflection: This problem is a great example of how recursion and backtracking work together in tree problems. Managing state carefully is key to collecting all valid paths without duplication. #LeetCode #BinaryTree #DFS #Backtracking #Java #DSA #CodingPractice
To view or add a comment, sign in
-
-
Day 10/100 Q 12 – Remove Duplicates from Sorted Array (DSA) Today’s problem was all about in-place array manipulation and two-pointer technique. 📌 Problem Statement Given a sorted integer array, remove duplicates in-place so that each unique element appears only once and return the count of unique elements. 🧠 Key Insight Since the array is already sorted, duplicates appear next to each other. Using two pointers, we can overwrite duplicates and keep all unique elements at the beginning of the array without using extra space. ⚙️ Approach Use one pointer to track the last unique element Traverse the array with another pointer Update the array when a new unique element is found ⏱ Complexity Time: O(n) Space: O(1) 💡 What I Learned How sorting simplifies duplicate problems Practical use of the two-pointer technique Writing space-optimized solutions 🔁 On to the next challenge! Consistency > Motivation 💪 #100DaysOfDSA #DSA #Arrays #TwoPointers #Java #ProblemSolving #LeetCode #CodingJourney #DailyLearning
To view or add a comment, sign in
-
-
Day 99/100 – #100DaysOfCode 🚀 | #Java #PrefixSum #HashMap ✅ Problem Solved: Continuous Subarray Sum (LeetCode 523) 🧩 Problem Summary: Given an integer array and an integer k, determine if the array has a continuous subarray of size at least 2 whose sum is a multiple of k. 💡 Approach Used: ✔ Used Prefix Sum + HashMap ✔ Key observation: If two prefix sums have the same remainder when divided by k, the subarray between them has a sum divisible by k. Steps: Maintain running sum Store (prefixSum % k) → earliest index in a HashMap If the same remainder appears again and the subarray length ≥ 2 → return true Initialize map with (0 → -1) to handle edge cases ⚙ Time Complexity: O(N) 📦 Space Complexity: O(K) (where K = number of unique remainders) ✨ Takeaway: Modular arithmetic paired with prefix sums is a powerful pattern for detecting divisible subarrays in linear time. #Java #LeetCode #PrefixSum #HashMap #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
𝟑𝐒𝐮𝐦 — 𝐖𝐡𝐲 𝐒𝐨𝐫𝐭𝐢𝐧𝐠 𝐂𝐡𝐚𝐧𝐠𝐞𝐬 𝐭𝐡𝐞 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐈𝐭𝐬𝐞𝐥𝐟 When I first approached 3Sum, I deliberately avoided sorting. I tried fixing one element and finding two others whose sum negates it. To support this, I even created a temporary sorted array for the remaining elements and applied two pointers there. It worked. But it was messy, slow, and generated duplicate triplets endlessly. To fix duplicates, I patched the solution: •sort the triplet •store it in a HashMap •check before adding Accepted. But the time complexity was terrible. 🧠 That’s when the real realization hit. Sorting the input array isn’t an optimization. It changes the structure of the problem. Once the array is sorted: •equal values form blocks •duplicates become visible •skipping an entire block becomes a correctness rule, not a hack Instead of deduplicating after generating answers, I prevent invalid information from being generated in the first place. This problem forced me to stop thinking in steps and start thinking in invariants and value blocks. Core insight: “Sorting isn’t a trick — it reshapes the search space.” Sharing the final solution that helped everything click for me: #DSA #ProblemSolving #LearnInPublic #ComputerScience #NeetCode #NeetCode150 #Java
To view or add a comment, sign in
-
-
🚀 LeetCode Problem Solved: Two Sum II (Input Array is Sorted) Implemented an optimized solution in Java using the Two Pointer technique. Since the array is already sorted, we can efficiently adjust pointers based on the current sum and find the required pair without using extra space. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) Source Code : https://lnkd.in/ew2_GMw9 This problem is a great example of how understanding constraints (sorted input) helps in designing efficient solutions. #LeetCode #Java #DSA #TwoPointers #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
LeetCode Practice - 1047. Remove All Adjacent Duplicates In String ✅ Key idea Instead of using a real stack: 🔷We use a character array 🔷And an integer pointer (top) 🔷This behaves exactly like a stack. 🧩 What we maintain 🔷char[] stack → stores final characters 🔷top → points to the last inserted character 🔷top = -1 → stack is empty #LeetCode #Java #StringHandling #CodingPractice #ProblemSolving #DSA #DeveloperJourney #TechLearning
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