Skip to content

Add fix for problem 2325 #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/com/fishercoder/solutions/_2325.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,29 @@ public String decodeMessage(String key, String message) {
return sb.toString();
}
}
public static class Solution2 {

public String decodeMessage(String key, String message) {
// put first occurrence of each char of key in hashmap, where k = char in key, v = incremental a - z alphabets

Map bucket = new HashMap<>();
char ch = 'a';
char keyArr[] = key.toCharArray();
StringBuilder result = new StringBuilder();

for(int i = 0; i < keyArr.length; i++) {
if (keyArr[i] != ' ' && !bucket.containsKey(keyArr[i])) {
bucket.put(keyArr[i], ch++);
}
}

// decode the message using the bucket
char msgArr[] = message.toCharArray();
for(int i = 0; i < msgArr.length; i++) {
if(msgArr[i] == ' ') result.append(" ");
else result.append(bucket.get(msgArr[i]));
}
return result.toString();
}
}
}
26 changes: 26 additions & 0 deletions src/test/java/com/fishercoder/_2325Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.fishercoder;

import com.fishercoder.solutions._2325;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class _2325Test {
private static _2325.Solution2 solution2;
private String key;
private String message;

@BeforeClass
public static void setup() {
solution2 = new _2325.Solution2();
}

@Test
public void test1() {
key = "the quick brown fox jumps over the lazy dog";
message = "vkbs bs t suepuv";
String actual = solution2.decodeMessage(key, message);
String expected = "this is a secret";
Assert.assertEquals(actual, expected);
}
}