-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodedString.java
More file actions
53 lines (39 loc) · 1.12 KB
/
Copy pathEncodedString.java
File metadata and controls
53 lines (39 loc) · 1.12 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
package quiz;
import java.util.Arrays;
/**
* @Author : yion
* @Date : 2017. 6. 16.
* @Description :
*/
public class EncodedString {
public static void main(String[] args) {
String s = "23#(2)24#25#26#23#(3)";
int[] result = frequency(s);
System.out.println(Arrays.toString(result));
}
private static int[] frequency(String s) {
int[] result = new int[26];
int size = s.length();
int i = 0;
while (i < size) {
int val = 0;
if (i + 2 >= size || s.charAt(i+2) != '#') {
val = s.charAt(i) - '0';
result[val - 1]++;
i++;
} else if (s.charAt(i+2) == '#'){
val = (s.charAt(i) - '0') * 10 + (s.charAt(i + 1) - '0');
result[val - 1]++;
i = i+3;
}
if (i < size) {
if (s.charAt(i) == '(') {
int frequen = s.charAt(i+1) - '0';
result[val-1] += frequen - 1;
i = i + 3;
}
}
}
return result;
}
}