I'm trying to find out if the given input is a valid parentheses or not. The input string is made of '(', ')', '{', '}', '[' and ']' .
An input string is valid if:
1.Open brackets must be closed by the same type of brackets.
2.Open brackets must be closed in the correct order.
3. empty strings are valid
However my code below using recursion is not working on the valid cases. It is suppose to go to the base case (when input is "") but instead goes to the return statement after the for loop.
class Solution {
public boolean validParen(String input) {
if(input.isEmpty()) {
return true;
}
else {
for (int i = 0; i < input.length() - 1; i++) {
if ((input.charAt(i) == '(' && input.charAt(i + 1) == ')') ||
(input.charAt(i) == '{' && input.charAt(i + 1) == '}') ||
(input.charAt(i) == '[' && input.charAt(i + 1) == ']')) {
input = input.substring(0, i) + input.substring(i + 2);
System.out.println("Input is " + input);
validParen(input);
}
}
return false;
}
}
public static void main(String[] args) {
Solution sol = new Solution();
//System.out.println(sol.validParen(""));
//System.out.println(sol.validParen("()")); // returns false for some reason
//System.out.println(sol.validParen("()[]{}")); // returns false for some reason
//System.out.println(sol.validParen("(]"));
//System.out.println(sol.validParen("([)]"));
//System.out.println(sol.validParen("{[]}")); // returns false for some reason
}
}
As said in the comment, you can consider use a stack. When current character is ( or { or [, put them in the stack. When current character is ) or } or ], check if there is the counterpart in the stack(for a valid input, it must exist) and pop it.
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("{[]}"));
}
}
have you tried replacing
validParen(input);
with
return validParen(input);
? Otherwise this line doesn't really do much ;)
From the call order point of view, it doesn't matter if you call method a() from a() or from anywhere else.
Let's look at a simple example
public int getOne() {
return 1;
}
public int getA(int a) {
/*
what you do here is call getOne(). The method will be called,
and it will produce some result, but this result will not be persisted
in any way, you will just go to the next line where the original a's
value will be returned
*/
getOne();
return a;
}
Is this case a bit clearer? Obviously if you call getA(2), 2 will be returned, not 1, even though getOne() is called internally - it's result is ignored.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With