Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit edf6621

Browse files
authored
Add fix for problem 2325 (fishercoder1534#173)
* add fix for 2325 * add fix for 2325
1 parent e2c2064 commit edf6621

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,29 @@ public String decodeMessage(String key, String message) {
3131
return sb.toString();
3232
}
3333
}
34+
public static class Solution2 {
3435

36+
public String decodeMessage(String key, String message) {
37+
// put first occurrence of each char of key in hashmap, where <k,v> 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+
}
3559
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}

0 commit comments

Comments
 (0)