Skip to content

Commit f509583

Browse files
committed
200318 algorithm
1 parent 2c64d2a commit f509583

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
int index = 0;
3+
public int solution(String s) {
4+
if(s.length()%2==1) return 0;
5+
char[] stack = new char[s.length()];
6+
int top = -1;
7+
stack[++top] = s.charAt(0);
8+
for(int i =1;i<s.length();i++){
9+
char prev = stack[top];
10+
char next = s.charAt(i);
11+
if(prev==next) {
12+
if(top==0){
13+
if(i+1<s.length()){
14+
stack[top] = s.charAt(i+1);
15+
i++;
16+
}else{
17+
top--;
18+
}
19+
}else{
20+
top--;
21+
}
22+
}else{
23+
stack[++top] = next;
24+
}
25+
}
26+
if(top==-1) return 1;
27+
else return 0;
28+
}
29+
}

0 commit comments

Comments
 (0)