i have an assessment here,and i provided a solution but when i run the code,the result is weird.
Here is the assessment:
"You are given a sequence of characters consisting of parentheses ( ) and brackets [ ].
A String of this type is said to be correct:
- if it is an empty or null string
- if the string A is correct ,(A) and [A] are correct
- if the string A and B are correct the concatenation AB is also
correct
input: The string contains at most 10000 characters.
Examples:[( )] is correct ,(( )[ ]) is correct,( [ ) ] is not correct,(( is not correct.
Implement the method check(String str) to check the correctness of a string of this type.
check returns true if the string is correct,false otherwise. "
So i did that code below:
public class Solution {
public static boolean check(String str) {
String str1 = null,str2 = null;
if(str==null|| str.length()==0) return true;
if (check(str1)==true && check(str2)==true) {
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(Solution.check("([])")); //true
System.out.println(Solution.check("()[]")); //true
System.out.println(Solution.check("([)]")); //false
System.out.println(Solution.check("((")); //false
System.out.println(Solution.check("[(()])")); //false
}
}
But i run it and i get:
true
true
true
true
true
How can i fix it?
question from:
https://stackoverflow.com/questions/65832198/implement-a-method-in-java-to-check-correctness 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…