🚀 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
Solving LeetCode 32 – Longest Valid Parentheses with Stack-Based Logic
More Relevant Posts
-
🚀 Day 80 of #100DaysOfCode Today, I solved LeetCode 61 – Rotate List, a problem focused on linked list manipulation and efficient pointer handling. 💡 Problem Overview: Given the head of a linked list, the task is to rotate the list to the right by k places. 🧠 Approach: ✔️ Calculated the length of the linked list ✔️ Connected the tail to the head to form a circular list ✔️ Found the new tail position using k % length ✔️ Broke the cycle to get the rotated list This approach avoids unnecessary rotations and ensures optimal performance. ⚡ Key Takeaways: Linked list problems require strong pointer manipulation Converting problems into circular structures can simplify logic Modulo operation helps handle large values of k efficiently 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(1) 🎯 Day 80 Insight: Consistency over time builds confidence in solving even complex problems efficiently. On to Day 100 🚀 #LeetCode #100DaysOfCode #DSA #LinkedList #ProblemSolving #CodingJourney #SoftwareDevelopment #Consistency #InterviewPrep
To view or add a comment, sign in
-
-
.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
-
🚀 LeetCode 219 — Contains Duplicate II | Solved ✅ Today I tackled a classic Sliding Window + Hashing problem. 🔍 Problem: Check if there exist two equal elements such that their index difference is at most k. 💡 Approach: Used a sliding window of size k with a hash set to track elements. If an element already exists in the window → duplicate found. ⚡ Complexity: Time: O(n) Space: O(k) ✨ Key Learning: Instead of tracking frequency, sometimes just tracking presence is enough — simplifying both logic and performance. Consistency is the real key 🔑 #LeetCode #DSA #CodingJourney #SlidingWindow #Hashing
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
-
-
🚀 Day 76 of #100DaysOfCode Today, I solved LeetCode 18 – 4Sum, a classic problem that extends the two-pointer technique to higher complexity. 💡 Problem Overview: Given an array, the task is to find all unique quadruplets that sum up to a target value. 🧠 Approach: ✔️ Sorted the array to simplify processing ✔️ Fixed two elements using nested loops ✔️ Applied the two-pointer technique for the remaining two elements ✔️ Carefully handled duplicates to ensure unique quadruplets This approach efficiently reduces unnecessary computations compared to brute force. ⚡ Key Takeaways: Sorting + Two Pointers is a powerful combination Avoiding duplicates is crucial in combination problems Breaking a complex problem into smaller parts simplifies logic 📊 Complexity Analysis: Time Complexity: O(n³) Space Complexity: O(1) (excluding output) Consistently pushing boundaries and solving more complex problems 🚀 #LeetCode #100DaysOfCode #DSA #TwoPointers #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 62 of #100DaysOfCode Today, I solved LeetCode 2075 – Decode the Slanted Ciphertext, a problem that focuses on matrix traversal and string manipulation. 💡 Problem Overview: Given an encoded text written in a slanted manner across a matrix, the task is to reconstruct the original message by decoding it correctly. 🧠 Approach: To solve this problem efficiently, I focused on: ✔️ Constructing the matrix from the given encoded string ✔️ Traversing diagonally to extract the original message ✔️ Handling trailing spaces carefully to get the correct output This approach ensures accurate decoding while maintaining simplicity. ⚡ Key Takeaways: Matrix traversal techniques are useful in string problems Diagonal traversal patterns often appear in encoding/decoding tasks Edge case handling (like trailing spaces) is crucial 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(n) Consistency and structured practice are driving continuous improvement 🚀 #LeetCode #100DaysOfCode #DSA #Strings #MatrixTraversal #ProblemSolving #CodingJourney #SoftwareDevelopment
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
-
-
My first “All Kill” in today’s LeetCode contest! 🚀 I gave the LeetCode Weekly Contest 498 and solve 4/4 questions. It was a relatively easier contest as most of the questions didn't test very high problem-solving or implementation skills, but were topic-based. If you are familiar with the underlying topics, you can solve these questions. I was lucky enough to be familiar with all these topics!! Here’s a brief breakdown of my approach: 1. Smallest Stable Index I (Easy) The constraints were small enough that brute force would work, but I noticed early that the 2nd question was the same as the first one and thus directly implemented an optimized approach in linear time. This way I could use the same code for the Q2. Just simply compute a prefix array for Max and suffix array for Min and get the first index that satisfies the conditions. We don't actually need a prefix array, the max can be calculated in the same loop as the loop to get the stable index, but to write the code faster i didn't think much and just implemented it with a prefix and suffix array. 2. Smallest Stable Index II (Medium) Same logic as Q1, just larger constraints, linear time solution works well. 3. Multi Source Flood Fill (Medium) Standard multi-source BFS. • Push all sources into the queue initially • Maintain a time matrix to track earliest arrival • If multiple colours reach a cell at the same time → choose the maximum colour 4. Count Good Integers on a Grid Path (Hard) A really interesting Digit DP + path simulation problem. Pretty standard. The only tricky part is to think of flattening the 2D grid • Convert each number into a 16-digit grid • Precompute the path positions from the directions • Apply digit DP with state tracking: • current index • last digit (to ensure non-decreasing sequence) • tight constraint • Count all valid numbers in range [l, r] Overall, a great contest and a confidence booster 💯 Now focusing on improving speed and handling tougher problems. #LeetCode #WeeklyContest #CompetitiveProgramming #DSA #ProblemSolving #LeetCodeContest #AllKill
To view or add a comment, sign in
-
-
🚀 Day 78 of #100DaysOfCode Today, I solved LeetCode 154 – Find Minimum in Rotated Sorted Array II, a problem that focuses on binary search and handling edge cases with duplicates. 💡 Problem Overview: Given a rotated sorted array that may contain duplicates, the goal is to find the minimum element efficiently. 🧠 Approach: ✔️ Applied modified binary search ✔️ Compared mid element with the right boundary ✔️ Carefully handled duplicate values to avoid incorrect elimination of search space This approach ensures correctness even when duplicates are present. ⚡ Key Takeaways: Binary search can be adapted for complex scenarios Duplicates introduce edge cases that must be handled carefully Choosing the correct condition is key to narrowing the search space 📊 Complexity Analysis: Time Complexity: O(log n) (average), O(n) (worst case due to duplicates) Space Complexity: O(1) Strengthening problem-solving with optimized approaches 🚀 #LeetCode #100DaysOfCode #DSA #BinarySearch #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 64 of #100DaysOfCode Today, I solved LeetCode 290 – Word Pattern, a problem that focuses on pattern matching and mapping relationships. 💡 Problem Overview: Given a pattern and a string, the task is to determine if the string follows the same pattern, ensuring a one-to-one mapping between characters in the pattern and words in the string. 🧠 Approach: To solve this problem efficiently, I focused on: ✔️ Using a hashmap to map pattern characters to words ✔️ Ensuring a bijection (one-to-one mapping) between pattern and words ✔️ Validating consistency throughout the traversal This approach ensures correctness while maintaining simplicity. ⚡ Key Takeaways: Hashmaps are powerful for mapping relationships Ensuring bijection is crucial in pattern problems Clean validation logic avoids edge-case errors 📊 Complexity Analysis: Time Complexity: O(n) Space Complexity: O(n) Building strong fundamentals one problem at a time 🚀 #LeetCode #100DaysOfCode #DSA #HashMap #ProblemSolving #CodingJourney #SoftwareDevelopment #InterviewPrep
To view or add a comment, sign in
-
Explore related topics
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