File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -31,5 +31,29 @@ public String decodeMessage(String key, String message) {
31
31
return sb .toString ();
32
32
}
33
33
}
34
+ public static class Solution2 {
34
35
36
+ public String decodeMessage (String key , String message ) {
37
+ // put first occurrence of each char of key in hashmap, where k = char in key, v = incremental a - z alphabets
38
+
39
+ Map <Character , Character > bucket = new HashMap <>();
40
+ char ch = 'a' ;
41
+ char keyArr [] = key .toCharArray ();
42
+ StringBuilder result = new StringBuilder ();
43
+
44
+ for (int i = 0 ; i < keyArr .length ; i ++) {
45
+ if (keyArr [i ] != ' ' && !bucket .containsKey (keyArr [i ])) {
46
+ bucket .put (keyArr [i ], ch ++);
47
+ }
48
+ }
49
+
50
+ // decode the message using the bucket
51
+ char msgArr [] = message .toCharArray ();
52
+ for (int i = 0 ; i < msgArr .length ; i ++) {
53
+ if (msgArr [i ] == ' ' ) result .append (" " );
54
+ else result .append (bucket .get (msgArr [i ]));
55
+ }
56
+ return result .toString ();
57
+ }
58
+ }
35
59
}
Original file line number Diff line number Diff line change
1
+ package com .fishercoder ;
2
+
3
+ import com .fishercoder .solutions ._2325 ;
4
+ import org .junit .Assert ;
5
+ import org .junit .BeforeClass ;
6
+ import org .junit .Test ;
7
+
8
+ public class _2325Test {
9
+ private static _2325 .Solution2 solution2 ;
10
+ private String key ;
11
+ private String message ;
12
+
13
+ @ BeforeClass
14
+ public static void setup () {
15
+ solution2 = new _2325 .Solution2 ();
16
+ }
17
+
18
+ @ Test
19
+ public void test1 () {
20
+ key = "the quick brown fox jumps over the lazy dog" ;
21
+ message = "vkbs bs t suepuv" ;
22
+ String actual = solution2 .decodeMessage (key , message );
23
+ String expected = "this is a secret" ;
24
+ Assert .assertEquals (actual , expected );
25
+ }
26
+ }
You can’t perform that action at this time.
0 commit comments