forked from fineanmol/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountandspeak.java
More file actions
48 lines (36 loc) · 1.24 KB
/
Copy pathcountandspeak.java
File metadata and controls
48 lines (36 loc) · 1.24 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
import java.util.HashMap;
import java.util.Map;
public class Solution{
public static String build(String str) {
Map<Character, Integer> fq = new HashMap<>();
StringBuilder ans = new StringBuilder();
for (int i = 0; i < str.length() - 1; i++) {
char num = str.charAt(i);
if (str.charAt(i) == str.charAt(i + 1)) {
fq.put(num, fq.getOrDefault(num, 0) + 1);
} else {
ans.append((char) ('0' + fq.getOrDefault(str.charAt(i), 0) + 1));
ans.append(str.charAt(i));
fq.put(str.charAt(i), 0);
}
}
int n = str.length() - 1;
ans.append((char) ('0' + fq.getOrDefault(str.charAt(n), 0) + 1));
ans.append(str.charAt(n));
return ans.toString();
}
public static String writeAsYouSpeak(int n) {
StringBuilder initial = new StringBuilder("1");
if (n == 1) {
return "1";
}
for (int i = 1; i < n; i++) {
initial = new StringBuilder(build(initial.toString()));
}
return initial.toString();
}
public static void main(String[] args) {
// Example usage:
System.out.println(writeAsYouSpeak(5));
}
}