The problem statement is as follows:
Given a string s containing only 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.
So Lets first thing you would need to know is the thing that the various pieces of enclosures are
opening = "[[("
closing = "])]"
So presently we can go through the genuine info string and check. In the event that the person is an initial section, we can push it onto a stack. In case it is shutting, we should check if our stack is unfilled or not. On the off chance that it is vacant, we can return False. Else, we check the latest thing we pushed onto the stack and contrast it and our present thing. Assuming they are coordinating with open and close sections, we can pop.
Java
import java.util.Stack;
class Solution {
public boolean validParen(String input) {
if (input.isEmpty()) {
return true;
} else {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < input.length(); i++) {
char current = input.charAt(i);
if (current == '(' || current == '[' || current == '{') {
stack.push(current);
} else {
if(stack.isEmpty()) {
return false;
}
char peekChar = stack.peek();
if ((current == ')' && peekChar != '(')
|| (current == '}' && peekChar != '{')
|| (current == ']' && peekChar != '[')) {
return false; // for a valid input, a close brackets must have an open brackets
} else {
stack.pop();
}
}
}
return true;
}
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.validParen(""));
System.out.println(sol.validParen("()"));
System.out.println(sol.validParen("()[]{}"));
System.out.println(sol.validParen("(]"));
System.out.println(sol.validParen("([)]"));
System.out.println(sol.validParen("{[]}"));
}
}
Python :
open_list
=
[
"["
,
"{"
,
"("
]
close_list
=
[
"]"
,
"}"
,
")"
]
# Python Function to check parentheses
def
check(myStr):
stack
=
[]
for
i
in
myStr:
if
i
in
open_list:
stack.append(i)
elif
i
in
close_list:
pos
=
close_list.index(i)
if
((
len
(stack) >
0
)
and
(open_list[pos]
=
=
stack[
len
(stack)
-
1
])):
stack.pop()
else
:
return
"Unbalanced"
if
len
(stack)
=
=
0
:
return
"Balanced"
else
:
return
"Unbalanced"
# Driver code
string
=
"{[]{()}}"
(string,
"-"
, check(string))
string
=
"[{}{})(]"
(string,
"-"
, check(string))
string
=
"((()"
(string,
"-"
,check(string))
Full Solution in JavaScript
You can use this code in Javascript to match the exact functionality above.
var isValid = function (s) { const obj = { "(": ")", "{": "}", "[": "]", } const stack = []; for (const paran of s) { if (obj.hasOwnProperty(paran)) { stack.push(paran) } else { const closeParan = stack.pop(); if (paran !== obj[closeParan]) { return false; } } } return stack.length === 0; };
For Reference
https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-python/
https://www.w3resource.com/python-exercises/class-exercises/python-class-exercise-3.php
About author
I am Saurabh Singh, coding enthusiastic, working in TCS. My technologies are JavaScript, Nodejs (Web Dev). I am open to working as a freelancing. please feel free to contact me below
https://www.linkedin.com/in/saurabh-singh777/
Email -: [email protected]
Be the first to comment on "Valid Parentheses |Java | Python | Javascript | LeetCode"