Skip to content

Commit 9f25d54

Browse files
refactor 1309
1 parent b46dcf6 commit 9f25d54

File tree

2 files changed

+81
-2
lines changed

2 files changed

+81
-2
lines changed

src/main/java/com/fishercoder/solutions/_1309.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
/**
47
* 1309. Decrypt String from Alphabet to Integer Mapping
58
*
@@ -33,9 +36,49 @@
3336
* */
3437
public class _1309 {
3538
public static class Solution1 {
39+
//TODO: very silly solution, optimze it
3640
public String freqAlphabets(String s) {
37-
//TODO: implement it
38-
return "";
41+
Map<String, String> map = new HashMap<>();
42+
map.put("1", "a");
43+
map.put("2", "b");
44+
map.put("3", "c");
45+
map.put("4", "d");
46+
map.put("5", "e");
47+
map.put("6", "f");
48+
map.put("7", "g");
49+
map.put("8", "h");
50+
map.put("9", "i");
51+
map.put("10#", "j");
52+
map.put("11#", "k");
53+
map.put("12#", "l");
54+
map.put("13#", "m");
55+
map.put("14#", "n");
56+
map.put("15#", "o");
57+
map.put("16#", "p");
58+
map.put("17#", "q");
59+
map.put("18#", "r");
60+
map.put("19#", "s");
61+
map.put("20#", "t");
62+
map.put("21#", "u");
63+
map.put("22#", "v");
64+
map.put("23#", "w");
65+
map.put("24#", "x");
66+
map.put("25#", "y");
67+
map.put("26#", "z");
68+
StringBuilder sb = new StringBuilder();
69+
for (int i = 0; i < s.length(); ) {
70+
if (Integer.parseInt("" + s.charAt(i)) == 1 && i + 1 < s.length() && i + 2 < s.length() && s.charAt(i + 2) == '#') {
71+
sb.append(map.get(s.substring(i, i + 3)));
72+
i += 3;
73+
} else if (Integer.parseInt("" + s.charAt(i)) == 2 && i + 1 < s.length() && i + 2 < s.length() && s.charAt(i + 2) == '#') {
74+
sb.append(map.get(s.substring(i, i + 3)));
75+
i += 3;
76+
} else {
77+
sb.append(map.get("" + s.charAt(i)));
78+
i++;
79+
}
80+
}
81+
return sb.toString();
3982
}
4083
}
4184
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1309;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _1309Test {
10+
private static _1309.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1309.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals("jkab", solution1.freqAlphabets("10#11#12"));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals("acz", solution1.freqAlphabets("1326#"));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals("y", solution1.freqAlphabets("25#"));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals("abcdefghijklmnopqrstuvwxyz", solution1.freqAlphabets("12345678910#11#12#13#14#15#16#17#18#19#20#21#22#23#24#25#26#"));
35+
}
36+
}

0 commit comments

Comments
 (0)