πDay 1. Parenthesis Checker π§
The problem can be found at the following link: Question Link
π‘ Problem Description:
Given a string s
, composed of different combinations of (
, )
, {
, }
, [
, ]
, verify the validity of the arrangement.
A string is valid if:
Open brackets must be closed by the same type of brackets.
Open brackets must be closed in the correct order.
π Example Walkthrough:
Example 1:
Input:
s = "[{()}]"
Output:
true
Explanation:
All brackets are correctly paired and well-formed.
Example 2:
Input:
s = "[()()]{}"
Output:
true
Explanation:
All brackets are well-formed.
Example 3:
Input:
s = "([]"
Output:
false
Explanation:
The expression is not balanced as there is a missing ')'
at the end.
Example 4:
Input:
s = "([{]})"
Output:
false
Explanation:
The expression is not balanced as there is a closing ]
before the closing }
.
Constraints:
$1 \leq |s| \leq 10^6$
s[i]
β{ '(', ')', '{', '}', '[', ']' }
π― My Approach:
Stack-Based Validation (O(N) Time, O(N) Space)
Iterate over the string and use a stack to track opening brackets.
When encountering a closing bracket:
If the stack is empty, return
false
.Check whether the top of the stack matches the expected opening bracket.
If matched, pop the stack. Otherwise, return
false
.
After iterating the string, the stack should be empty for a valid expression.
Algorithm Steps:
Use a stack to store open brackets
{[(
.Iterate through the string:
If
s[i]
is an opening bracket, push it onto the stack.If
s[i]
is a closing bracket:Check if the stack is empty (invalid case).
Compare the top element of the stack with
s[i]
.If it matches, pop the stack. Otherwise, return
false
.
After the loop, return true if the stack is empty, else false.
π Time and Auxiliary Space Complexity
Expected Time Complexity:
O(N)
, since each character is processed once in the loop.Expected Auxiliary Space Complexity:
O(N)
, in the worst case, all characters are pushed onto the stack.
π Solution Code
Code (C++)
class Solution {
public:
bool isBalanced(string& s) {
stack<char> st;
for (char c : s) {
if (c == '(' || c == '{' || c == '[') st.push(c);
else if (st.empty() || abs(st.top() - c) > 2) return false;
else st.pop();
}
return st.empty();
}
};
Code (Java)
class Solution {
static boolean isBalanced(String s) {
Stack<Character> st = new Stack<>();
for (char c : s.toCharArray())
if ("({[".indexOf(c) >= 0) st.push(c);
else if (st.isEmpty() || Math.abs(st.peek() - c) > 2) return false;
else st.pop();
return st.isEmpty();
}
}
Code (Python)
class Solution:
def isBalanced(self, s):
st = []
for c in s:
if c in "({[": st.append(c)
elif not st or abs(ord(st[-1]) - ord(c)) > 2: return False
else: st.pop()
return not st
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated