Day 38 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Print Bracket Number We were given a string str. It contains brackets. Task was to assign a number to each bracket. Each pair gets the same number. Based on order of opening. Let’s understand it simply. ◾️Imagine you are assigning IDs to tasks. ◾️Every time a new task starts, you give it a new ID. ◾️When that task ends, it keeps the same ID. ◾️Multiple tasks can be nested. ◾️But each one has its own number. That’s exactly how brackets behave. 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Initialize a counter = 0. 🔹️Traverse the string. 🔹️If opening bracket: ▪️Increase counter ▪️Push it into stack ▪️Print the counter 🔹️If closing bracket: ▪️Take top value from stack ▪️Print it ▪️Pop from stack Each pair gets same number. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack helps in tracking nested structures. ▫️Assigning IDs during traversal simplifies pairing logic. ▫️LIFO nature perfectly matches bracket problems. ▫️Nested patterns are easier with stack-based thinking. Day 38 completed. Stack understanding getting stronger 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
Day 38 of #50DaysOfCode: Assigning IDs to Brackets with Stack
More Relevant Posts
-
.Day 47 of my #50DaysOfCode challenge is done ✅ 📌 Problem Solved Bracket Validity (Valid Parentheses) We were given a string s. It contains only brackets: (), {}, [] Task was to check whether the string is valid. Valid means: ✔️ Same type of brackets ✔️ Correct order ✔️ Every opening has a matching closing 💻 Approach (Using Stack) 🔹️Create an empty stack. 🔹️Traverse the string. 🔹️If opening bracket → push into stack. 🔹️If closing bracket: ▪️Check if stack is empty → invalid ▪️Check top of stack ▪️If matching → pop ▪️Else → invalid 🔹️At the end, stack should be empty. If empty → valid Else → not valid 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(n) 📚 What I learned today: ▫️Stack is the best fit for bracket matching problems. ▫️Checking empty stack avoids runtime errors. ▫️Matching pairs logic must be handled carefully. ▫️Order of brackets is more important than count. Day 47 completed. Revising stack patterns again 🚀 #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
🚀 Day 58 of #100DaysOfCode Today, I solved LeetCode 2840 – Check if Strings Can be Made Equal With Operations II, a problem that builds upon string manipulation and constraint-based transformations. 💡 Problem Overview: Given two strings, the objective is to determine whether they can be made equal using a defined set of swap operations. The challenge lies in understanding which positions can influence each other. 🧠 Approach: To efficiently solve this problem, I focused on: ✔️ Identifying independent index groups based on allowed operations ✔️ Separating characters into even and odd indexed groups ✔️ Comparing sorted/grouped characters from both strings This ensures correctness while maintaining optimal performance. ⚡ Key Takeaways: Identifying independent groups simplifies complex transformations Sorting/grouping techniques are effective for comparison problems Understanding constraints leads directly to optimal solutions 📊 Complexity Analysis: Time Complexity: O(n log n) Space Complexity: O(n) Small improvements every day lead to significant growth over time 🚀 #LeetCode #100DaysOfCode #DSA #Strings #ProblemSolving #CodingJourney #SoftwareDevelopment
To view or add a comment, sign in
-
-
🚀 Day 66 of #100DaysOfCode Today, I solved LeetCode 73 – Set Matrix Zeroes, a classic problem that tests in-place matrix manipulation and optimization techniques. 💡 Problem Overview: Given a matrix, if any cell contains 0, its entire row and column must be set to 0. The challenge is to perform this efficiently without using excessive extra space. 🧠 Approach: To solve this optimally, I focused on: ✔️ Using the first row and first column as markers ✔️ Tracking whether the first row/column initially contained zero ✔️ Updating the matrix in-place based on these markers This avoids using additional space and achieves optimal performance. ⚡ Key Takeaways: In-place algorithms help reduce space complexity Using matrix itself as storage is a powerful optimization trick Handling edge cases (first row/column) is critical 📊 Complexity Analysis: Time Complexity: O(n × m) Space Complexity: O(1) Improving problem-solving skills one day at a time 🚀 #LeetCode #100DaysOfCode #DSA #Matrix #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
Day 97/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 456 – 132 Pattern(Medium) 🧠 Approach: Traverse the array from right to left while maintaining a stack. Use a variable to track the “middle” element (the ‘2’ in 132 pattern) and check if a valid pattern exists. 💻 Solution: class Solution: def find132pattern(self, nums: List[int]) -> bool: stack = [] third = float('-inf') for i in range(len(nums) - 1, -1, -1): if nums[i] < third: return True while stack and nums[i] > stack[-1]: third = stack.pop() stack.append(nums[i]) return False ⏱ Time | Space: O(n) | O(n) 📌 Key Takeaway: Using a monotonic stack while iterating backwards helps efficiently detect complex patterns in linear time. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 50 of my #50DaysOfCode challenge is done ✅🎉 📌 Problem Solved Reorder Linked List We were given a linked list. Task was to reorder it like: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 … Not just reverse. Rearrange in a specific pattern. 💻 Approach 🔹️Find the middle of the linked list. 🔹️Reverse the second half. 🔹️Merge both halves alternately. Step by step: 🔹️Use slow and fast pointers to find middle 🔹️Reverse second half of list 🔹️Merge nodes one by one Careful pointer handling needed. 📊 Complexity Analysis Time Complexity: O(n) Space Complexity: O(1) 📚 What I learned today: ▫️Linked list problems often combine multiple steps. ▫️Finding middle + reversing + merging is a common pattern. ▫️Pointer manipulation needs careful attention. ▫️Breaking problem into parts makes it manageable. 🎯 50 Days Completed Started with basic patterns. Reached linked lists, stacks, recursion, sliding window. Some days were easy. Some were confusing. But I showed up every day. That’s the biggest win. This challenge was not just about coding. It was about consistency and discipline. #50DaysOfCode #CodingChallenge #Consistency #LearningInPublic
To view or add a comment, sign in
-
Day 89/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 64 – Maximum Gap (Medium) 🧠 Approach: Sort the array and compute the maximum difference between consecutive elements. 💻 Solution: class Solution: def maximumGap(self, nums: List[int]) -> int: if len(nums) < 2: return 0 nums.sort() max_gap = 0 for i in range(1, len(nums)): max_gap = max(max_gap, nums[i] - nums[i-1]) return max_gap ⏱ Time | Space: O(n log n) | O(1) 📌 Key Takeaway: Sorting helps identify the maximum gap by comparing adjacent elements, though linear-time bucket-based solutions can further optimize performance. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
🚀 Day 70 of #100DaysOfCode 📌 LeetCode Q3: 3Sum 💡 Problem: Find all unique triplets in the array which gives the sum of 0. 🧠 Approach I Used: - First, sorted the array - Fixed one element - Applied two-pointer technique for the remaining part - Skipped duplicates to avoid repeated triplets ⚡ Key Insight: Instead of brute force O(n³), using sorting + two pointers reduces it to O(n²) ❗ Edge Cases Handled: - Duplicate values - No valid triplets - Negative + positive mix ⏱ Complexity: - Time: O(n²) - Space: O(1) (excluding result) 🔥 Takeaway: Two-pointer + sorting = deadly combo for array problems 💯 💬 Open to feedback & better approaches! #Day70 #100DaysOfCode #LeetCode #DSA #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 5️⃣ /15 — Consistency Challenge 🚀 Today’s problem: LeetCode 1855 — Maximum Distance Between a Pair of Values Solved using a clean two-pointer approach. 🔹 Problem Idea: We need to find the maximum value of j - i such that: i <= j nums1[i] <= nums2[j] Both arrays are non-increasing (sorted in descending order), which makes this a perfect two-pointer problem. 🔹 Approach (Two Pointers): Start two pointers: i = 0 for nums1 j = 0 for nums2 Traverse while both pointers are in bounds: If nums1[i] <= nums2[j] ✅ Valid pair found Update answer with j - i Move j forward to try for a larger distance Else (nums1[i] > nums2[j]) ❌ Invalid pair Move i forward to reduce nums1[i] and try to make condition valid 🔹 Time Complexity: Each pointer moves at most once through its array 👉 O(n + m) 🔹 Space Complexity: 👉 O(1) Clean logic, efficient solution, and another step in the consistency journey 💯 #LeetCode #DSA #TwoPointers #Consistency #CodingJourney #LearnInPublic
To view or add a comment, sign in
-
-
🚀 Day 45/100 – LeetCode Challenge 🔗 Problem: Linked List Components (LeetCode 817) 💡 Difficulty: Medium Today’s problem was all about understanding connected components in a linked list using a smart approach. 🧠 Key Insight: Instead of checking every possible combination, I used a HashSet for quick lookups. A component is counted only when a sequence ends, i.e., when the current node is in nums but the next node is not. ⚡ Approach: Convert nums into a set for O(1) lookup Traverse the linked list Count only when a component ends 📈 Time Complexity: O(n) 📦 Space Complexity: O(n) ✨ What I learned: Sometimes, solving problems efficiently is about identifying where something ends rather than where it starts. Consistency is key 🔥 Halfway through the journey — staying committed! #Day45 #100DaysOfCode #LeetCode #DataStructures #LinkedList #CodingJourney #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
Day 88 of #100DaysOfCode Today I solved "Construct Binary Search Tree from Preorder Traversal" on LeetCode using Recursion + Range Validation. Key Idea: In preorder traversal, elements are processed as: Root → Left → Right We can rebuild the BST by maintaining a valid range (lower, upper) for each node. Approach: • Start with range (-∞, +∞) • Pick current value → create node • For left subtree → range becomes (lower, root value) • For right subtree → range becomes (root value, upper) • Move index forward while constructing tree This ensures we build a valid BST without searching Concepts Used: • Binary Search Tree (BST) • Recursion • Preorder Traversal • Range constraints Time Complexity: O(n) Space Complexity: O(h) This problem shows how powerful range-based recursion can be for tree construction Understanding traversal patterns is unlocking new levels #Day88 #100DaysOfCode #LeetCode #BST #Recursion #Cpp #CodingJourney
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