File tree Expand file tree Collapse file tree 2 files changed +81
-2
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +81
-2
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
+ import java .util .HashMap ;
4
+ import java .util .Map ;
5
+
3
6
/**
4
7
* 1309. Decrypt String from Alphabet to Integer Mapping
5
8
*
33
36
* */
34
37
public class _1309 {
35
38
public static class Solution1 {
39
+ //TODO: very silly solution, optimze it
36
40
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 ();
39
82
}
40
83
}
41
84
}
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments