Repository navigation
Expand file tree
/
Copy pathBB1.java
More file actions
83 lines (66 loc) · 2.09 KB
/
Copy pathBB1.java
File metadata and controls
83 lines (66 loc) · 2.09 KB
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
import java.util.*;
public class BB1 {
private class Letter {
private char letter;
private int count;
public Letter(char letter, int count) {
this.letter = letter;
this.count = count;
}
public char getLetter() {
return this.letter;
}
public int getCount() {
return this.count;
}
public void setCount(int count) {
this.count = count;
}
}
private String crush(String s) {
StringBuilder sb = new StringBuilder();
Stack<Letter> st = new Stack<>();
int threshold = 3;
for (int i = 0; i < s.length(); i++) {
if (st.isEmpty() || st.peek().getLetter() != s.charAt(i)) {
if (!st.isEmpty() && st.peek().getCount() >= threshold) {
st.pop();
}
if (!st.isEmpty() && st.peek().getLetter() == s.charAt(i)) {
Letter l = st.pop();
l.setCount(l.getCount() + 1);
st.push(l);
} else {
st.push(new Letter(s.charAt(i), 1));
}
} else {
Letter l = st.pop();
l.setCount(l.getCount() + 1);
st.push(l);
}
}
if (st.peek().getCount() >= threshold) {
st.pop();
}
while (!st.isEmpty()) {
Letter l = st.pop();
for (int i = 0; i < l.getCount(); i++) {
sb.append(l.getLetter());
}
}
return sb.reverse().toString();
}
public static void main(String[] args) {
BB1 candy = new BB1();
String toCrush1 = "aaabbbbbacddddd";
String toCrush2 = "deeeeedbbcccccbdaa";
String toCrush3 = "aaabbbacd";
// Should print "ac"
System.out.println(candy.crush(toCrush1));
// Should print "aa"
System.out.println(candy.crush(toCrush2));
// Should print "cd"
System.out.println(candy.crush(toCrush3));
return;
}
}