-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1p6.java
More file actions
33 lines (30 loc) · 860 Bytes
/
Copy pathq1p6.java
File metadata and controls
33 lines (30 loc) · 860 Bytes
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
import java.util.*;
import java.lang.*;
public class q1p6{
public static void main(String[] args){
String test = "aaaaaaaaaaaaacssscdfcccdddddsssaaadssscccdd";
System.out.println(check(test));
}
// T: O(n)
// S: O(n)
// A: if StringBuilder was used, T should be O(n^2)
public static String check(String str){
StringBuffer sb = new StringBuffer();
int cnt = 0;
char buf = '\u0000';
for(int i = 0; i<str.length(); i++){
if(str.charAt(i) != buf){
if(i != 0)
sb.append("" + buf+cnt);
buf = str.charAt(i);
cnt = 1;
}else{
cnt++;
}
if(i == str.length()-1){
sb.append(""+buf+cnt);
}
}
return new String(sb);
}
}