🚀 50 Important Coding Questions – Question 34/50 🔹 Valid Parentheses | LeetCode A fundamental Stack-based problem that checks whether parentheses in a string are properly balanced. 📌 Problem Statement Given a string containing just the characters ( ) { } [ ], determine if the input string is valid. A string is valid if: ✔ Open brackets are closed by the same type ✔ Open brackets are closed in the correct order ✔ Every closing bracket has a matching opening bracket Example: Input: s = "()[]{}" Output: true 💡 Approach 1️⃣ Traverse the string character by character 2️⃣ Use a stack to store opening brackets 3️⃣ When a closing bracket appears, check the top of the stack 4️⃣ If they form a valid pair → pop from stack 5️⃣ Otherwise → invalid string ✔ Stack helps track unmatched opening brackets. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 📌 LeetCode Result: ✔ Accepted ⚡ Efficient stack-based implementation 🧠 Concepts Strengthened ✔ Stack data structure ✔ String traversal ✔ Matching pair logic ✔ Edge case handling 📍 Question 34 of 50 in my “50 Important Coding Questions” series. Every problem solved builds stronger algorithmic thinking 💯 👉 Question 35 coming next! #DSA #LeetCode #Stack #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
Valid Parentheses Stack Problem
More Relevant Posts
-
🚀 50 Important Coding Questions – Question 46/50 🔹 Decode String | LeetCode A classic Stack + String manipulation problem involving nested encoding patterns. 📌 Problem Statement Given an encoded string s, decode it using the rule: k[encoded_string] → repeat encoded_string k times The encoding may be nested. Example: Input s = "3[a2[c]]" Output "accaccacc" Explanation: a2[c] -> acc 3[acc] -> accaccacc 💡 Approach We use a stack to handle nested patterns. Steps: 1️⃣ Traverse the string 2️⃣ Push characters until ] is found 3️⃣ When ] appears: • Pop characters until [ → get substring • Extract the number (k) • Repeat the substring k times • Push back into stack 4️⃣ Continue until string ends 5️⃣ Build final result from stack ⏱ Time Complexity: O(n * k) (due to repeated strings) 📦 Space Complexity: O(n) 📌 LeetCode Result ✔ Accepted ⚡ Efficient stack-based decoding 🧠 Concepts Strengthened ✔ Stack for nested structures ✔ String parsing ✔ Handling multi-digit numbers ✔ Recursion-like behavior using stack 📍 Question 46 of 50 in my “50 Important Coding Questions” series. Only 4 problems left — final stretch! 💯🔥 👉 Question 47 coming next! #DSA #LeetCode #Stack #Strings #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
🚀 50 Important Coding Questions – Question 50/50 🔹 Subsets (Power Set) | LeetCode A classic problem to master recursion + backtracking. 📌 Problem Statement Given an integer array, return all possible subsets (the power set). 💡 Approach Used (Backtracking / Recursion) I solved this using a recursive backtracking approach where: 👉 At every index, we have two choices Include the current element Exclude the current element 👉 Maintain a temporary list to build subsets 👉 When we reach the end of the array, store the current subset 🔍 How it works 👉 Traverse the array index by index 👉 For each element: First include it and move forward Then backtrack (remove it) and explore exclusion 👉 This forms a decision tree (include / exclude) ⏱ Time Complexity: O(2ⁿ) 📦 Space Complexity: O(n) (recursion stack) 📌 LeetCode Result ✔ Accepted ⚡ Runtime: 0 ms 🧠 Concepts Strengthened ✔ Backtracking fundamentals ✔ Recursion tree thinking ✔ Include–Exclude pattern ✔ State management (push & pop) 🏁 50/50 Questions Completed! 💯🔥 Consistency = Results 💪 From basics → advanced patterns 🚀 #DSA #LeetCode #Backtracking #Recursion #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
🚀 50 Important Coding Questions – Question 49/50 🔹 Power of Three | LeetCode A simple yet important problem to understand recursion and mathematical patterns. 📌 Problem Statement Given an integer n, return true if it is a power of three. Otherwise, return false. 💡 Approach Used (Recursive) I solved this using a recursive approach where: 👉 If the number is less than or equal to 0 → return false 👉 If the number becomes 1 → it is a valid power of three 👉 If the number is not divisible by 3 → return false 👉 Otherwise, recursively divide the number by 3 🔍 How it works 👉 Continuously divide the number by 3 👉 Check divisibility at every step 👉 Stop when it either becomes 1 (valid) or fails condition ⏱ Time Complexity: O(log₃ n) 📦 Space Complexity: O(log n) (recursion stack) 📌 LeetCode Result ✔ Accepted ⚡ Runtime: 0 ms 🧠 Concepts Strengthened ✔ Recursion fundamentals ✔ Mathematical reasoning ✔ Divide and reduce approach ✔ Base case handling 📍 Question 49 of 50 in my “50 Important Coding Questions” series. Only 1 question left 💯🔥 👉 Final Question coming next! #DSA #LeetCode #Recursion #Math #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
🚀 50 Important Coding Questions – Question 48/50 🔹 Reverse Linked List | LeetCode A fundamental problem to understand recursion and pointer manipulation in linked lists. 📌 Problem Statement Given the head of a singly linked list, reverse the list and return the new head. 💡 Approach Used (Recursive) I solved this using a recursive approach where: 👉 First, recursion goes till the last node (base case) 👉 Then while returning, each node reverses its pointer 👉 Current node makes its next node point back to itself 👉 Finally, the original links are broken to avoid cycles 🔍 How it works 👉 Traverse till the end using recursion 👉 While backtracking, reverse the direction of links 👉 Gradually build the reversed list ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) (recursion stack) 📌 LeetCode Result ✔ Accepted ⚡ Runtime: 0 ms 🧠 Concepts Strengthened ✔ Recursion with pointers ✔ Linked list manipulation ✔ Backtracking in recursion ✔ Base case importance 📍 Question 48 of 50 in my “50 Important Coding Questions” series. Only 2 questions left 💯🔥 👉 Question 49 coming next! #DSA #LeetCode #LinkedList #Recursion #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
🚀 50 Important Coding Questions – Question 35/50 🔹 Min Stack | LeetCode A classic Stack design problem where we maintain the minimum element in constant time. 📌 Problem Statement Design a stack that supports the following operations: ✔ push(x) ✔ pop() ✔ top() ✔ getMin() — return the minimum element in the stack All operations must run in O(1) time. 💡 Approach To maintain the minimum efficiently: 1️⃣ Keep track of the current minimum value 2️⃣ When pushing a new smaller element, store an encoded value in the stack 3️⃣ Update the minimum accordingly 4️⃣ When popping, decode the value to restore the previous minimum This trick allows us to maintain minimum tracking without using an extra stack. ⏱ Time Complexity: O(1) for all operations 📦 Space Complexity: O(n) 📌 LeetCode Result: ✔ Accepted ⚡ Constant time operations for push, pop, top, and getMin 🧠 Concepts Strengthened ✔ Stack data structure ✔ Space optimization techniques ✔ Encoding / decoding trick ✔ Designing efficient data structures 📍 Question 35 of 50 in my “50 Important Coding Questions” series. Consistency in solving problems is building stronger fundamentals 💯 👉 Question 36 coming next! #DSA #LeetCode #Stack #DataStructures #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 13 of My Coding Journey Today I worked on an interesting problem: 527 Valid Word Abbreviation 💡 👉 The challenge was to check whether a given abbreviation correctly represents a word. It involved handling: Character matching ✅ Skipping characters using numbers 🔢 Edge cases like leading zeros ❌ 💻 Key Logic I Used: Two pointers (i for word, j for abbreviation) If characters match → move both pointers If digit found → build number and skip characters in word If invalid case → return false ✨ Code Snippet: class Solution { public: bool validWordAbbreviation(string word, string abbr) { int i=0, j=0; while(i<word.size() && j<abbr.size()) { if(abbr[j]=='0') return false; if(word[i]==abbr[j]) { i++; j++; } else if(isalpha(abbr[j])) return false; else { int num=0; while(j<abbr.size() && isdigit(abbr[j])) { num = (num*10) + (abbr[j]-'0'); j++; } i += num; } } return i==word.size() && j==abbr.size(); } }; 🧠 What I Learned: How to efficiently parse strings with mixed characters & numbers Importance of handling edge cases like "01" ❗ Two-pointer technique is 🔥 for string problems 📌 Consistency > Perfection See you tomorrow with Day 14! 💪 #100DaysOfCode #DSA #Cpp #CodingJourney #LeetCode #ProblemSolving
To view or add a comment, sign in
-
🚀 50 Important Coding Questions – Question 44/50 🔹 Evaluate Reverse Polish Notation | LeetCode A classic Stack problem used to evaluate expressions written in postfix notation. 📌 Problem Statement You are given an array of strings tokens representing an arithmetic expression in Reverse Polish Notation (RPN). Evaluate the expression and return the result. Allowed operators: ➕ Addition ➖ Subtraction ✖ Multiplication ➗ Division Example: Input tokens = ["2","1","+","3","*"] Output 9 Explanation: (2 + 1) * 3 = 9 💡 Approach We use a stack to evaluate the expression. Algorithm: 1️⃣ Traverse the tokens array 2️⃣ If the token is a number → push it onto the stack 3️⃣ If the token is an operator • Pop two numbers from the stack • Apply the operation • Push the result back into the stack 4️⃣ The final stack element is the answer ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 📌 LeetCode Result ✔ Accepted ⚡ Efficient stack-based solution 🧠 Concepts Strengthened ✔ Stack operations ✔ Expression evaluation ✔ Postfix (RPN) notation ✔ Arithmetic operation handling 📍 Question 44 of 50 in my “50 Important Coding Questions” series. Only 6 problems left to complete the challenge 💯 👉 Question 45 coming next! #DSA #LeetCode #Stack #Algorithms #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
📌 LeetCode Daily Challenge — Day 10 Remember yesterday's problem? Here's the plot twist 🔄 Yesterday I solved: 3145. Stable Binary Arrays I — Medium Today I'm solving: 3146. Stable Binary Arrays II — Hard Same problem. Same logic. Same code. But the constraints jumped from 200 to 1000 and that one change decides whether your solution passes or fails! Topic: Dynamic Programming, Prefix Sum 📌 What Changed: Problem 1 → zero, one ≤ 200 → DP table = 40K states Problem 2 → zero, one ≤ 1000 → DP table = 1M states If your solution has any hidden inner loop, Problem 2 catches it immediately with TLE 💥 🧠 Why The Same Code Still Works: 🔹 The solution I wrote yesterday already used the subtraction trick 🔹 Instead of looping over run lengths (O(limit) per state), I subtract the boundary case (O(1) per state) 🔹 dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1] - dp[i-limit-1][j][1] 🔹 That one subtraction collapses the entire run length dimension 🔹 Total complexity → O(zero × one), scales perfectly even when constraints jump 5x 🏃 Quick Recap: Input: zero = 1, one = 1, limit = 2 Valid arrays with one 0 and one 1: "01" ✅ "10" ✅ return 2 ✅ Same input, same output but Problem 2 would reject any O(n²×limit) approach instantly. ⏱️ Time Complexity: O(zero × one) Every state computed in O(1) no inner loops 📦 Space Complexity: O(zero × one) 3D DP with last digit dimension = 2 💭 Here's the lesson: LeetCode often pairs a Medium and Hard version of the same problem. Medium lets you verify your logic even with a slightly naive approach. Hard cranks the constraints and forces you to write the truly optimized version. If you solve Medium with the optimal approach from day one, the Hard version is just copy-paste ✅ That's why thinking about optimization early always pays off! Same article from yesterday applies here the code hasn't changed one bit: https://lnkd.in/gMRVXyKx Have you ever had a solution that scaled perfectly from Medium to Hard? Drop it in the comments 👇 See you in the next problem 👋 #java #SoftwareEngineer #CodingInterview #DynamicProgramming #BackendDeveloper
To view or add a comment, sign in
-
-
🚀 50 Important Coding Questions – Question 37/50 🔹 Next Greater Element II | LeetCode An extension of the Next Greater Element problem, but with a circular array twist. 📌 Problem Statement Given a circular integer array, return the next greater number for every element. The next greater number of a number x is the first greater number to its right in the array. Since the array is circular, the search may continue from the beginning. If no greater element exists → return -1. Example: Input nums = [1,2,1] Output [2,-1,2] 💡 Approach We use a Monotonic Decreasing Stack. Key idea: 1️⃣ Traverse the array twice (2n) to simulate circular behavior 2️⃣ Use a stack to store indices 3️⃣ Remove elements from stack while they are ≤ current element 4️⃣ The stack top becomes the next greater element 5️⃣ Store results in the answer array This avoids checking the array repeatedly. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(n) 📌 LeetCode Result: ✔ Accepted ⚡ Efficient stack-based implementation 🧠 Concepts Strengthened ✔ Monotonic Stack ✔ Circular array handling ✔ Stack optimization ✔ Efficient traversal techniques 📍 Question 37 of 50 in my “50 Important Coding Questions” series. Step by step building stronger DSA fundamentals 💯 👉 Question 38 coming next! #DSA #LeetCode #Stack #MonotonicStack #CodingInterview #ProblemSolving #CPlusPlus #TechJourney
To view or add a comment, sign in
-
-
🚀 Dive into the world of Object-Oriented Programming with this lesson data! 🌟 In simple terms, OOP is a programming paradigm that organizes code into objects with properties and behaviors. It allows developers to create modular, reusable code, leading to better organization and efficiency in their projects. Plus, it promotes code readability and scalability, making collaboration easier for teams. 🛠️💡 🔍 Here's a breakdown to get you started: 1️⃣ Define a class 2️⃣ Create an object 3️⃣ Initialize object properties 4️⃣ Define object methods 5️⃣ Access object properties and methods ```python class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): return "Woof!" my_dog = Dog("Buddy", 2) print(my_dog.name) print(my_dog.bark()) ``` Pro Tip: Encapsulate data within objects to ensure data integrity and security! 🔒 Common Mistake Alert! 👀 Don't forget to use proper naming conventions when defining classes and objects. Be clear and consistent to maintain code readability. 🌟 How do you plan to implement OOP in your next project? Share your thoughts below! 💬 🌐 View my full portfolio and more dev resources at tharindunipun.lk 🚀 #ObjectOrientedProgramming #CodeOrganization #ReusableCode #PythonDevelopment #CodingTips #DeveloperCommunity #CodeQuality #LearnToCode #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