.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
Day 47: Validating Bracket Validity with Stack Approach
More Relevant Posts
-
🚀 Day 79 of #100DaysOfCode Today, I solved LeetCode 32 – Longest Valid Parentheses, a challenging problem that focuses on stack-based logic and string processing. 💡 Problem Overview: Given a string containing only '(' and ')', the goal is to find the length of the longest valid (well-formed) parentheses substring. 🧠 Approach: ✔️ Used a stack-based approach to track indices ✔️ Initialized stack with -1 to handle edge cases ✔️ For every closing bracket, popped from stack ✔️ Calculated valid substring length using current index and stack top This approach efficiently tracks valid sequences and avoids reprocessing. ⚡ Key Takeaways: Stack is powerful for handling matching problems Index-based tracking simplifies substring calculations Handling edge cases (like invalid starting brackets) is crucial 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(n) Solving hard problems step by step and improving every day 🚀 #LeetCode #100DaysOfCode #DSA #Stack #Strings #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep #HardProblems
To view or add a comment, sign in
-
-
Day 26/75 🚀 Solved Decode String (LeetCode 394) today! ✅ All 34/34 test cases passed ⚡ Runtime: 0 ms (Beats 100%) 💾 Memory: 8.22 MB (Beats ~99%) 🔍 Approach: Used a stack-based approach to decode the string. ✔️ Traverse the string character by character ✔️ If character is not ‘]’ → push into result ✔️ When ‘]’ is found: • Extract substring inside brackets • Get the number (repeat count) before '[' • Repeat the substring and append back This continues until the entire string is decoded. 💡 Key Learning: Whenever you see nested patterns (like brackets) → think of stack. It helps manage order and structure efficiently. Consistency + patience = clean solutions 💯 #LeetCode #CPP #DSA #ProblemSolving #CodingJourney #75DaysOfCode #Focused
To view or add a comment, sign in
-
-
🚀 Day 64/100 – LeetCode Challenge. 🔍 Problem: Valid Parentheses. Today’s problem was all about checking whether a string of brackets is valid or not. Sounds simple… but the trick lies in handling it efficiently! 💡 Approach Used: Stack (LIFO) 👉 Idea: Push opening brackets → ( { [ On closing bracket → check top of stack If match → pop Else → invalid ❌ 🧠 Key Insight: Stack helps maintain the correct order of brackets. The last opened bracket must be the first one to close. ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(n) 📌 Takeaway: Whenever you see problems involving matching pairs / nested structures, think of using a stack! #100DaysOfCode #DSA #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 92/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 347 – Top K Frequent Elements(Medium) 🧠 Approach: Count the frequency of each element using a hashmap, then sort the elements based on frequency in descending order and pick the top k. 💻 Solution: class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: freq = {} for num in nums: freq[num] = freq.get(num, 0) + 1 sorted_items = sorted(freq.items(), key=lambda x: x[1], reverse=True) return [item[0] for item in sorted_items[:k]] ⏱ Time | Space: O(n log n) | O(n) 📌 Key Takeaway: Hashmaps combined with sorting provide a simple way to solve frequency-based problems, though heaps or bucket sort can optimize performance further. #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Day 67/100 — #100DaysOfCode Today , Worked on two problems: 𝐅𝐢𝐧𝐝𝐢𝐧𝐠 𝐦𝐢𝐧𝐢𝐦𝐮𝐦 𝐚𝐧𝐝 𝐦𝐚𝐱𝐢𝐦𝐮𝐦 𝐢𝐧 𝐚𝐧 𝐚𝐫𝐫𝐚𝐲 𝐅𝐢𝐧𝐝𝐢𝐧𝐠 𝐟𝐢𝐫𝐬𝐭 𝐚𝐧𝐝 𝐥𝐚𝐬𝐭 𝐨𝐜𝐜𝐮𝐫𝐫𝐞𝐧𝐜𝐞 𝐨𝐟 𝐚 𝐭𝐚𝐫𝐠𝐞𝐭 𝐢𝐧 𝐚 𝐬𝐨𝐫𝐭𝐞𝐝 𝐚𝐫𝐫𝐚𝐲 𝟏. 𝐌𝐢𝐧 & 𝐌𝐚𝐱 𝐢𝐧 𝐀𝐫𝐫𝐚𝐲(Brute Force clarity) At first glance, it’s simple — but the real learning was the mental model. Instead of guessing whether a number is min or max, I learned: Always keep a reference (minVal, maxVal) Compare every element with that reference Update accordingly Core idea: Don’t judge a number in isolation, compare it with what you already know. Time complexity: O(n) because every element must be seen. 𝟐. 𝐅𝐢𝐫𝐬𝐭 & 𝐋𝐚𝐬𝐭 𝐏𝐨𝐬𝐢𝐭𝐢𝐨𝐧 𝐨𝐟 𝐄𝐥𝐞𝐦𝐞𝐧𝐭 (Binary Search shift) My initial instinct was: Traverse array Maybe sort But that completely breaks the constraint. Since the array is already sorted and O(log n) is required, the correct approach is: Use Binary Search Not once, but twice: First occurrence → move left Last occurrence → move right Core idea: When sorted + fast search is required → think Binary Search, not loops. I’m starting to see a pattern: Problems are not about code first They are about choosing the right approach Wrong thinking → correct code won’t save you Right thinking → code becomes easy On to Day 68
To view or add a comment, sign in
-
-
🚀 Day 65/100 – LeetCode Challenge. 🔍 Problem: Remove All Adjacent Duplicates in String. At first glance, this problem looks like a simple string manipulation task… but the real magic lies in using the right approach! 💡 Approach Used: Stack (via string) 👉 Idea: Traverse the string If current character = last character of result → remove it Else → add it 🧠 Key Insight: We simulate a stack using a string. Last added character = top of stack → result.back() ⚡ Time Complexity: O(n) ⚡ Space Complexity: O(n) 📌 Takeaway: Whenever you see adjacent duplicate removal / pair cancellation, think of stack pattern instantly! 💬 Example: Input: "abbaca" Output: "ca" #Day65 #100DaysOfCode #DSA #LeetCode #CodingJourney
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 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
-
-
Solved LeetCode 1047 — Remove All Adjacent Duplicates In String 🧹 🔍 Problem • Given a string, repeatedly remove adjacent duplicate characters • Continue the process until no duplicates remain • Return the final cleaned string ⚙️ Approach (Stack) • Traverse the string character by character • Use a stack to keep track of characters • If current character equals stack top → remove (pop) • Otherwise → push the character • Build the result from the stack 💡 Key Learning • Using stack for pair cancellation problems • Efficient removal of duplicates in a single pass • Avoiding repeated string operations ⏱ Complexity • Time: O(n) ⏳ • Space: O(n) 📦 Consistency continues 🚀 #100DaysOfCode #LeetCode #DSA #Stack #Strings #ProblemSolving #Consistency
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