Given a string s
containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Example Cases:-
Example 1:
Input: s = "()" Output: true
Example 2:
Input: s = "()[]{}" Output: true
Example 3:
Input: s = "(]" Output: false
kjkj
Valid Parentheses solution
var isValid = function(s) {
s=s.split("")//Here we are changing the string into an array using split
let res="" // storing the result if the condition is true. why we are using
res instead of directly returning the res because suppose my first condition is
true and after that second condition is false because of which we are storing
in res so that end res will be stored
let stack=[]; // using an empty stack to store the element of an "s"
for(let i=0; i<=s.length-1; i++){
let input=s[i]
if(stack.length==0 &&(s[i]==")"|| s[i]=="}"||s[i]=="]")){
return false;
} //Here we are checking the stack if my stack is empty and my element
is one of the above so it will return the false
testCase="}" suppose this test case at starting it will become invalid
parentheses because there is no "{" element so it will become invalid
else{
if(input=="("|| input=="{"||input=="["){
stack.push(input); // will push the element into the stack
}
else{
let output=stack.pop()// will pop out the element from the stack and
store the element in the output variable
if(output=="[" &&(input==")" || input=="}")){
return false //Here we are checking the condition of my output
is same or not if not return false;
} else if(output=="{" &&(input==")" || input=="]")){
return false; //same
}
else if(output=="(" &&(input=="}" || input=="]")){
return false;//same
}else {
res="true"
}
}
}
}
if(stack.length>=1){
return false; // here checking the length of my stack
suppose testcase=> [[[] here my last condition will become
true but the first two remain in the stack so
we are checking the length
}else{
return res;
}
};
clear code
var isValid = function(s) {
s=s.split("")
let res=""
let stack=[];
for(let i=0; i<=s.length-1; i++){
let input=s[i]
if(stack.length==0 &&(s[i]==")"|| s[i]=="}"||s[i]=="]")){
return false;
}else{
if(input=="("|| input=="{"||input=="["){
stack.push(input);
}
else{
let output=stack.pop()
if(output=="[" &&(input==")" || input=="}")){
return false
} else if(output=="{" &&(input==")" || input=="]")){
return false;
}
else if(output=="(" &&(input=="}" || input=="]")){
return false;
}else {
res="true"
}
}
}
}
if(stack.length>=1){
return false;
}else{
return res;
}
};
Detailed Explanation
Initialization:
- We first convert the string into an array of characters using split("").
- We then initialize an empty stack to keep track of the opening brackets.
Iterate through the String:
- For each character in the string, we check if it is an opening bracket ((, {, or [).
- If it is, we push it onto the stack.
Handle Closing Brackets:
- If we encounter a closing bracket and the stack is empty, it means there is no corresponding opening bracket, so we return false.
- If the stack is not empty, we pop the last opening bracket from the stack and check if it matches the current closing bracket. If it does not match, we return false.
Final Check:
- After iterating through the string, if the stack is not empty, it means there are unmatched opening brackets, so we return false.
- If the stack is empty, all brackets are properly matched and nested, so we return true.
Conclusion
This approach ensures that the string is validated efficiently using a stack, adhering to the rules of proper bracket closure and nesting. By following this guide, you can confidently handle bracket validation in JavaScript, ensuring robust and error-free code. For more tips on coding and algorithm optimization, make sure to follow our blog and stay updated with the latest programming practices!
Tags:
programming