|
@@ -1,6 +1,5 @@
|
1
|
1
|
package io.zipcoder;
|
2
|
2
|
|
3
|
|
-import java.util.ArrayList;
|
4
|
3
|
import java.util.Stack;
|
5
|
4
|
|
6
|
5
|
public class ParenChecker {
|
|
@@ -9,47 +8,47 @@ public class ParenChecker {
|
9
|
8
|
final char openParan = '(';
|
10
|
9
|
final char openCurly = '{';
|
11
|
10
|
final char openBrack = '[';
|
|
11
|
+ final char openCompa = '<';
|
|
12
|
+ final char openDQuot = '"';
|
|
13
|
+ final char openSQuot = '\'';
|
12
|
14
|
|
13
|
15
|
final char closeParan = ')';
|
14
|
16
|
final char closeCurly = '}';
|
15
|
17
|
final char closeBrack = ']';
|
|
18
|
+ final char closeCompa = '>';
|
|
19
|
+ final char closeDQuot = '"';
|
|
20
|
+ final char closeSQuot = '\'';
|
16
|
21
|
|
17
|
22
|
ParenChecker() {
|
18
|
23
|
stack = new Stack<Character>();
|
19
|
24
|
}
|
20
|
25
|
|
21
|
26
|
public boolean isBalancedParens(String s) {
|
22
|
|
- // boolean returnvalue = false;
|
23
|
27
|
for (char c : charArr = s.toCharArray()) {
|
24
|
|
-
|
25
|
|
- if (isParanCurlyBrack(c)) {
|
26
|
|
-
|
|
28
|
+ if (isOpenOrClose(c)) {
|
27
|
29
|
if (isOpening(c)) {
|
28
|
|
- stack.push(c);
|
|
30
|
+ if (!stack.empty() && isQuoteMatch(c)){
|
|
31
|
+ stack.pop();
|
|
32
|
+ } else {
|
|
33
|
+ stack.push(c);
|
|
34
|
+ }
|
29
|
35
|
} else if (stack.empty()) {
|
30
|
|
- // returnvalue = false;
|
31
|
36
|
return false;
|
32
|
|
- // break;
|
33
|
37
|
} else if (isClosing(c)) {
|
34
|
38
|
if (isMatch(c)) {
|
35
|
39
|
stack.pop();
|
36
|
40
|
} else {
|
37
|
|
- //returnvalue = false;
|
38
|
|
- // break;
|
39
|
41
|
return false;
|
40
|
42
|
}
|
41
|
43
|
}
|
42
|
|
-
|
43
|
44
|
}
|
44
|
|
-
|
45
|
45
|
}
|
46
|
46
|
return stack.empty();
|
47
|
|
- // return returnvalue;
|
48
|
47
|
}
|
49
|
48
|
|
50
|
49
|
public boolean isOpening(char c){
|
51
|
50
|
boolean returnValue = false;
|
52
|
|
- if (c == openParan || c == openBrack || c == openCurly){
|
|
51
|
+ if (c == openParan || c == openBrack || c == openCurly || c == openCompa || c == openDQuot || c == openSQuot){
|
53
|
52
|
returnValue = true;
|
54
|
53
|
}
|
55
|
54
|
return returnValue;
|
|
@@ -57,7 +56,7 @@ public class ParenChecker {
|
57
|
56
|
|
58
|
57
|
public boolean isClosing(char c){
|
59
|
58
|
boolean returnValue = false;
|
60
|
|
- if (c == closeParan || c == closeCurly || c == closeBrack){
|
|
59
|
+ if (c == closeParan || c == closeCurly || c == closeBrack || c == closeCompa || c == closeDQuot || c == closeSQuot){
|
61
|
60
|
returnValue = true;
|
62
|
61
|
}
|
63
|
62
|
return returnValue;
|
|
@@ -65,28 +64,36 @@ public class ParenChecker {
|
65
|
64
|
|
66
|
65
|
public boolean isMatch(char c){
|
67
|
66
|
boolean returnValue;
|
68
|
|
- if (c == closeParan && stack.peek() == openParan){
|
69
|
|
- returnValue = true;
|
70
|
|
- }
|
71
|
|
- else if (c == closeBrack && stack.peek() == openBrack){
|
|
67
|
+ if (
|
|
68
|
+ (c == closeParan && stack.peek() == openParan) ||
|
|
69
|
+ (c == closeBrack && stack.peek() == openBrack) ||
|
|
70
|
+ (c == closeCurly && stack.peek() == openCurly) ||
|
|
71
|
+ (c == closeDQuot && stack.peek() == openDQuot) ||
|
|
72
|
+ (c == closeSQuot && stack.peek() == openSQuot) ||
|
|
73
|
+ (c == closeCompa && stack.peek() == openCompa) ){
|
72
|
74
|
returnValue = true;
|
|
75
|
+ } else {
|
|
76
|
+ returnValue = false;
|
73
|
77
|
}
|
74
|
|
- else if (c == closeCurly && stack.peek() == openCurly){
|
|
78
|
+ return returnValue;
|
|
79
|
+ }
|
|
80
|
+
|
|
81
|
+ public boolean isQuoteMatch(char c){
|
|
82
|
+ boolean returnValue;
|
|
83
|
+ if ((c == closeDQuot && stack.peek() == openDQuot) || (c == closeSQuot && stack.peek() == openSQuot) ){
|
75
|
84
|
returnValue = true;
|
76
|
|
- }
|
77
|
|
- else {
|
|
85
|
+ } else {
|
78
|
86
|
returnValue = false;
|
79
|
87
|
}
|
80
|
88
|
return returnValue;
|
81
|
89
|
}
|
82
|
90
|
|
83
|
|
- public boolean isParanCurlyBrack(char c) {
|
84
|
|
- if (c == openParan || c == openBrack || c == openCurly ||
|
85
|
|
- c == closeParan || c == closeCurly || c == closeBrack)
|
86
|
|
- {
|
|
91
|
+
|
|
92
|
+ public boolean isOpenOrClose(char c) {
|
|
93
|
+ if (c == openParan || c == openBrack || c == openCurly || c == openCompa || c == openDQuot || c == openSQuot ||
|
|
94
|
+ c == closeParan || c == closeCurly || c == closeBrack || c == closeCompa || c == closeDQuot || c == closeSQuot) {
|
87
|
95
|
return true;
|
88
|
|
- }
|
89
|
|
- else {
|
|
96
|
+ } else {
|
90
|
97
|
return false;
|
91
|
98
|
}
|
92
|
99
|
}
|