-
Notifications
You must be signed in to change notification settings - Fork 1
/
DecodeTheMessage.java
37 lines (32 loc) · 1.02 KB
/
DecodeTheMessage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/decode-the-message/
public class DecodeTheMessage {
private final String key;
private final String message;
public DecodeTheMessage(String key, String message) {
this.key = key;
this.message = message;
}
public String solution() {
char[] table = new char[128];
char original = 'a';
for (int i = 0; i < key.length(); i++) {
int curr = key.charAt(i);
if (curr != ' ') {
if (table[curr] == 0) {
table[curr] = original++;
}
}
}
StringBuilder result = new StringBuilder();
for (int i = 0; i < message.length(); i++) {
char curr = message.charAt(i);
if (curr != ' ') {
result.append(table[curr] != 0 ? table[curr] : curr);
} else {
result.append(curr);
}
}
return result.toString();
}
}