----Continuing my DSA practice, today I solved another hard problem.---- * Problem: Longest Valid Parentheses (32) * Approach: I solved this using a stack-based approach. The idea is to keep track of the indices of parentheses while traversing the string. When encountering "(", its index is pushed onto the stack. When encountering ")", we pop from the stack. If the stack becomes empty, we push the current index as a base reference. Otherwise, we calculate the length of the valid substring using the difference between the current index and the index at the top of the stack. * Key Learning: This problem helped me better understand how stacks can be used to track valid parentheses ranges and compute substring lengths efficiently. These kinds of problems is improving my understanding of stack patterns and string processing . #LeetCode #DSA #Java #ProblemSolving #CodingJourney
Longest Valid Parentheses Solution with Stack Approach
More Relevant Posts
-
🔁 DSA Practice: Reverse String (LeetCode Problem) Today I practiced the Reverse String problem, a fundamental question that helps build a strong understanding of arrays and two-pointer techniques. In this problem, we are given a character array and the goal is to reverse the string in-place, meaning we must modify the original array without using extra memory. 💡 Key Concept: Two-Pointer Technique One pointer starts from the beginning of the array. Another pointer starts from the end of the array. We swap the characters and move the pointers toward the center until they meet. 📌 Why this problem is important Improves understanding of array manipulation Introduces the two-pointer approach Helps practice in-place algorithms with O(1) space complexity ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Consistent practice with such problems strengthens problem-solving skills and builds a solid foundation for more advanced DSA challenges. #DSA #LeetCode #Java #ProblemSolving #CodingPractice #SoftwareEngineering
To view or add a comment, sign in
-
-
🔥 Day 353 – Daily DSA Challenge! 🔥 Problem: 🔍 Single Element in a Sorted Array Given a sorted array where every element appears twice except one, find that single element in O(log n) time. 💡 Key Insight — Binary Search on Pairs In a perfect paired array: Pairs start at even indices Pattern breaks at the single element We use binary search to detect where this pattern breaks. 🧠 Core Observation Before the single element: pairs → (even, odd) After the single element: pairs shift → (odd, even) So we normalize mid: if mid is odd → make it even ⚡ Algorithm Logic ✅ Find mid ✅ Make mid even ✅ Compare: If nums[mid] == nums[mid+1] → move right Else → move left This narrows down to the single element. ⚙️ Complexity ✅ Time Complexity: O(log n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why do we force mid to be even? 2️⃣ How would you solve this using XOR in O(n)? 3️⃣ What if elements appear thrice except one? #DSA #Day353 #LeetCode #BinarySearch #Arrays #Optimization #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 27 Today’s focus: Binary Search on modified arrays. Problem solved: • Search in Rotated Sorted Array (LeetCode 33) Concepts used: • Binary Search • Identifying sorted halves • Conditional search space reduction Key takeaway: This problem extends binary search to a rotated sorted array, where the array is not fully sorted but divided into two sorted parts. At each step, we: • Find the mid element • Check which half (left or right) is sorted • Decide whether the target lies in the sorted half • Eliminate the other half This allows us to still achieve O(log n) time complexity. Continuing to strengthen fundamentals and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 53 of My DSA Journey Today I solved a problem on searching an element in a rotated sorted array with duplicates. The array is originally sorted but rotated at some pivot. The task is to determine whether a given target value exists in the array. 🔎 My Approach Instead of directly jumping to complex techniques, I used a simple linear search approach: Traverse through the array from start to end. Compare each element with the target value. If a match is found, return true. If the loop finishes without finding the target, return false. 💡 Key Takeaway Sometimes starting with a simple and correct solution is important before optimizing. While more efficient approaches like modified binary search can reduce time complexity, this straightforward method helps clearly understand the problem first. ⏱ Time Complexity: O(n) Every day I’m improving my problem-solving skills in Java and Data Structures & Algorithms step by step. #Day53 #DSA #Java #ProblemSolving #CodingJourney #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
Day 68 of My DSA Journey Today’s problem: Symmetric Tree 🌳 Problem Statement Given a binary tree, check whether it is a mirror of itself (symmetric around its center). Key Insight A tree is symmetric if: Left subtree is a mirror of the right subtree Compare nodes in a cross manner: Left → Left with Right → Right Left → Right with Right → Left 🧠 Approach Use recursion to compare two nodes at a time Base cases: If both nodes are null → symmetric If one is null → not symmetric Check: Values are equal Outer and inner pairs match ⚡ Complexity Time: O(n) Space: O(h) (recursion stack) ✨ What I Learned This problem improved my understanding of recursion and how to think in terms of mirror structures instead of normal traversal. Consistency is the key 🔑 — one problem at a time! #DSA #Java #BinaryTree #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Diving into Linked Lists: Part 1 of my Java DSA Series Taking the next step in my algorithms journey by moving from arrays to dynamic memory allocation. Working with Linked Lists requires a completely different mental model, heavily relying on node references and pointer manipulation. To make sure I fully grasp the mechanics, I am splitting this topic into two parts. Here is what I implemented from scratch in Part 1: -Core Operations: Building the Node class and logic to Add, Remove, and Print elements at various positions. -Search Algorithms: Implementing both Iterative and Recursive search methods. -Reversal: Coding an in-place reversal algorithm using standard pointer manipulation. -Palindrome Checker: Utilizing the Slow and Fast pointer (Tortoise and Hare) approach to find the midpoint and verify palindromes. Getting comfortable with pointers has been a challenging but rewarding shift in problem-solving. Part 2 will be coming soon. #DSA #Java #LinkedList #Algorithms #ProblemSolving #SoftwareEngineering #OpenLearning
To view or add a comment, sign in
-
🚀 Day 6 – DSA Practice Solved Remove Duplicates from Sorted Array today. 📌 Problem: Given a sorted array, remove duplicates in-place such that each element appears only once and return the new length. 💡 Insight: Use the two-pointer approach — one pointer tracks unique elements, while the other scans the array to update values in-place. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) Strengthening my understanding of in-place array manipulation and pointer techniques. #Java #DSA #LeetCode #TwoPointers #CodingInterview
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 35 Today’s focus: Binary Search with index patterns. Problem solved: • Single Element in a Sorted Array (LeetCode 540) Concepts used: • Binary Search • Index parity (even/odd pattern) • Search space reduction Key takeaway: The array is sorted and every element appears twice except one. A key observation: Before the single element, pairs start at even indices After the single element, this pattern breaks. Using binary search: • If mid is even and nums[mid] == nums[mid + 1], the single element lies on the right side • Else, it lies on the left side (including mid) By leveraging this pattern, we can find the answer in O(log n) time and O(1) space. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 24 Today’s focus: Prefix Sum for range queries. Problem solved: • Count Vowel Strings in Ranges (LeetCode 2559) Concepts used: • Prefix Sum • Efficient range query processing • String boundary checks Key takeaway: The goal is to count how many strings in a given range start and end with a vowel. A direct approach would check each query range individually, leading to higher time complexity. Instead, we preprocess using a prefix sum array: • Mark each word as 1 if it starts and ends with a vowel, else 0 • Build a prefix sum over this array For any query [l, r], the answer can be found in O(1) using: prefix[r] - prefix[l - 1] This reduces the overall complexity significantly when handling multiple queries. This problem highlights how prefix sum is extremely useful for repeated range-based queries. Continuing to strengthen fundamentals and consistency in DSA problem solving. #DSA #Java #LeetCode #CodingJourney
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