Repository navigation
Expand file tree
/
Copy pathDecodetheMessage.java
More file actions
56 lines (45 loc) · 1.75 KB
/
Copy pathDecodetheMessage.java
File metadata and controls
56 lines (45 loc) · 1.75 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// LeetCode 2325. Decode the Message
import java.util.HashMap;
public class DecodetheMessage {
public String decodeMessage(String key, String message) {
String Cleankey=key.replaceAll("\\s+","");
HashMap<Character,Character> map=new HashMap<>();
StringBuilder sb=new StringBuilder();
int alphacount=0;
// this to maping the letters
for(char c: Cleankey.toCharArray()){
if(!map.containsKey(c)){
map.put(c,(char)('a'+ alphacount++));
}
else if(alphacount==26){
break;
}
}
// to assign the values
for(char c :message.toCharArray()){
if(c == ' '){
sb.append(c);
}
else{
sb.append(map.get(c));
}
}
return sb.toString();
}
}
// Example 1:
// Input: key = "the quick brown fox jumps over the lazy dog", message = "vkbs bs t suepuv"
// Output: "this is a secret"
// Explanation: The diagram above shows the substitution table.
// It is obtained by taking the first appearance of each letter in "the quick brown fox jumps over the lazy dog".
// Example 2:
// Input: key = "eljuxhpwnyrdgtqkviszcfmabo", message = "zwx hnfx lqantp mnoeius ycgk vcnjrdb"
// Output: "the five boxing wizards jump quickly"
// Explanation: The diagram above shows the substitution table.
// It is obtained by taking the first appearance of each letter in "eljuxhpwnyrdgtqkviszcfmabo".
// Constraints:
// 26 <= key.length <= 2000
// key consists of lowercase English letters and ' '.
// key contains every letter in the English alphabet ('a' to 'z') at least once.
// 1 <= message.length <= 2000
// message consists of lowercase English letters and ' '.