-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path020_ValidParentheses20.java
202 lines (177 loc) · 5.53 KB
/
020_ValidParentheses20.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();boolean check =false;
for(int i = 0 ; i < s.length(); i++){
if(stack.isEmpty()){
if(s.charAt(i) == ']' || s.charAt(i) == ')' || s.charAt(i) == '}'){
check = false;
return check;
}
}
if(s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '[' ){
stack.push(s.charAt(i));
continue;
}
else{
char ch = s.charAt(i);
switch(ch){
// -------------------------------------
case ']':
if(!stack.isEmpty()){
if(stack.pop() == '['){
check = true;
}
else{
check = false;
return check;
}
}
break;
// --------------------------------
case '}':
if(!stack.isEmpty()){
if(stack.pop ()== '{'){
check = true;
}
else{
check = false;
return check;
}
}
break;
// ---------------------------
case ')':
if(!stack.isEmpty()){
if(stack.pop() == '('){
check = true;
}
else{
check = false;
return check;
}
}
break;
// ---------------------------------
}}}
if(!stack.isEmpty()) {
check = false;
}
return check;
}
}
****************************************************************************************
class Solution {
public boolean isValid(String str) {
if(str == null || str.equals("") || str.length() == 1) {
return false;
}
Stack<Character> s = new Stack<Character>();
char[] chars = str.toCharArray();
for(int i=0; i< chars.length; i++) {
char c = chars[i];
if(c == '(' || c == '[' || c == '{') {
s.push(c);
continue;
}
if(s.isEmpty()) {
return false;
}
if(c == ')') {
char top = s.peek();
if(top == '[' || top == '{') {
return false;
}
} else if(c == ']') {
char top = s.peek();
if(top == '(' || top == '{') {
return false;
}
} else if(c == '}') {
char top = s.peek();
if(top == '(' || top == '[') {
return false;
}
}
s.pop();
}
return s.isEmpty();
}
}
*********************************************************************************
class Solution {
public boolean isValid(String s) {
Stack<Character> st = new Stack<Character>();
if(s.length() ==1) return false;
for(int i=0; i<s.length(); i++){
char inChar = s.charAt(i);
if(inChar=='{'||inChar=='['||inChar=='('){
st.push(inChar);
}else if(inChar==')'){
boolean val = handleClosing(st,'(');
if(val ==false){
return false;
}
}
else if(inChar=='}'){
boolean val = handleClosing(st,'{');
if(val ==false){
return false;
}
}
else if(inChar==']'){
boolean val = handleClosing(st,'[');
if(val ==false){
return false;
}
}
}
return st.isEmpty();
}
public static boolean handleClosing(Stack<Character> st,char corres ){
if(st.size()==0){
return false;
}
else if(st.peek()!=corres){
return false;
}else{
st.pop();
return true;
}
}
}
*********************************************************************************************
class Solution {
public boolean isValid(String s) {
Stack<Character> brackets = new Stack<Character>();
int length = s.length();
for (int i=0;i<length;i++){
switch (s.charAt(i)){
case '(':
brackets.push('(');
break;
case '{':
brackets.push('{');
break;
case '[':
brackets.push('[');
break;
case ')':
if(brackets.empty() || brackets.peek() != '(')
return false;
brackets.pop();
break;
case '}':
if(brackets.empty() || brackets.peek() != '{')
return false;
brackets.pop();
break;
case ']':
if(brackets.empty() || brackets.peek() != '[')
return false;
brackets.pop();
break;
}
}
return brackets.empty();
}
}