-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathModifyString.java
50 lines (48 loc) · 1.21 KB
/
ModifyString.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
class Pair{
char c;
int count;
Pair(char c,int count) {
this.c = c;
this.count = count;
}
}
class Solution {
public static String reduced_String(int k, String s) {
if(k == 1) {
String ans = "";
return ans;
}
Stack<Pair> st = new Stack<Pair>();
int l = s.length();
for(int i=0;i<l;i++) {
if(st.size() == 0) {
st.push(new Pair(s.charAt(i),1));
continue;
}
if(st.peek().c == s.charAt(i)) {
Pair p = st.peek();
st.pop();
p.count += 1;
if(p.count == k) {
continue;
} else {
st.push(p);
}
}
else {
st.push(new Pair(s.charAt(i),1));
}
}
StringBuilder ans= new StringBuilder();
while(st.size() > 0)
{
char c = st.peek().c;
int cnt = st.peek().count;
while(cnt-- > 0)
ans.append(String.valueOf(c));
st.pop();
}
ans.reverse();
return ans.toString();
}
}