-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidParentheses.java
54 lines (48 loc) · 2.1 KB
/
ValidParentheses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*******************************************************************************
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']',
determine if the input string is valid.
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. Every close bracket has a corresponding open bracket of the same type.
****************************************************************************** */
public class validParentheses {
public static void main(String[] args) {
//only parentheses {} or [] or (), no other characters. String length from 0 to 10000;
System.out.println("true - " + isValid("{}[]()"));
System.out.println("true - " + isValid("{[()]}"));
System.out.println("true - " + isValid("()"));
System.out.println("true - " + isValid("({(({})([()]){()})([()]{})[()]})"));
System.out.println("false - " + isValid("[[]"));
System.out.println("false - " + isValid("{{()}}}"));
System.out.println("false - " + isValid("}{"));
System.out.println("false - " + isValid("{"));
System.out.println("false - " + isValid(""));
}
public static boolean isValid(String str) {
int i = 0;
int len = str.length();
int char1, char2;
if (2 <= len && len <= 10000) {
StringBuilder newStr = new StringBuilder(str);
while (i < len) {
if ((i + 1) == len)
return false;
char1 = (int)newStr.charAt(i);
char2 = (int)newStr.charAt(i + 1);
if ((char1 == 40 && char2 == 41) || (char1 == 123 && char2 == 125) || (char1 == 91 && char2 == 93)) {
newStr.deleteCharAt(i + 1);
newStr.deleteCharAt(i);
len = newStr.length();
if (i > 0)
i--;
}
else
i++;
}
if (i == 0)
return true;
}
return false;
}
}