Skip to content

Commit 9bc7dfa

Browse files
solves #2325: Decode the Message in java
1 parent 3f7f66d commit 9bc7dfa

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -747,15 +747,15 @@
747747
| 2315 | [Count Asterisks](https://leetcode.com/problems/count-asterisks) | [![Java](assets/java.png)](src/CountAsterisks.java) [![Python](assets/python.png)](python/successfull_pairs_of_spells_and_potions.py) | |
748748
| 2316 | [Count Unreachable Pairs of Nodes in an Undirected Graph](https://leetcode.com/problems/count-unreachable-pairs-of-nodes-in-an-undirected-graph) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
749749
| 2319 | [Check if Matrix Is X-Matrix](https://leetcode.com/problems/check-if-matrix-is-x-matrix) | [![Java](assets/java.png)](src/CountAsterisks.java) | |
750-
| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
751-
| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
752-
| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
750+
| 2325 | [Decode the Message](https://leetcode.com/problems/decode-the-message) | [![Java](assets/java.png)](src/DecodeTheMessage.java) | |
751+
| 2331 | [Evaluate Boolean Binary Tree](https://leetcode.com/problems/evaluate-boolean-binary-tree) | | |
752+
| 2335 | [Minimum Amount of Time to Fill Cups](https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups) | | |
753753
| 2439 | [Minimize Maximum of Array](https://leetcode.com/problems/minimize-maximum-of-array) | [![Python](assets/python.png)](python/minimize_maximum_of_array.py) | |
754-
| 2341 | [Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
755-
| 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
754+
| 2341 | [Maximum Number of Pairs in Array](https://leetcode.com/problems/maximum-number-of-pairs-in-array) | | |
755+
| 2347 | [Best Poker Hand](https://leetcode.com/problems/best-poker-hand) | | |
756756
| 2348 | [Number of Zero-Filled Subarrays](https://leetcode.com/problems/number-of-zero-filled-subarrays) | [![Python](assets/python.png)](python/number_of_zero_filled_subarrays.py) | |
757-
| 2351 | [First Letter to Appear Twice](https://leetcode.com/problems/first-letter-to-appear-twice) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
758-
| 2357 | [Make Array Zero by Subtracting Equal Amounts](https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts) | [![Python](assets/python.png)](python/count_unreachable_pair_of_node_in_an_undirectable_graph.py) | |
757+
| 2351 | [First Letter to Appear Twice](https://leetcode.com/problems/first-letter-to-appear-twice) | | |
758+
| 2357 | [Make Array Zero by Subtracting Equal Amounts](https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts) | | |
759759
| 2360 | [Longest Cycle in a Graph](https://leetcode.com/problems/longest-cycle-in-a-graph) | [![Python](assets/python.png)](python/longest_cycle_in_graph.py) | |
760760
| 2363 | [Merge Similar Items](https://leetcode.com/problems/merge-similar-items) | | |
761761
| 2367 | [Number of Arithmetic Triplets](https://leetcode.com/problems/number-of-arithmetic-triplets) | | |

src/DecodeTheMessage.java

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// https://leetcode.com/problems/decode-the-message
2+
// T: O(|key| + |message|)
3+
// S: O(1)
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class DecodeTheMessage {
9+
public String decodeMessage(String key, String message) {
10+
final Map<Character, Character> mapping = getSubstitutionMap(key);
11+
final StringBuilder result = new StringBuilder();
12+
System.out.println(mapping);
13+
14+
for (int index = 0 ; index < message.length() ; index++) {
15+
if (message.charAt(index) == ' ') {
16+
result.append(' ');
17+
} else {
18+
result.append(mapping.get(message.charAt(index)));
19+
}
20+
}
21+
22+
return result.toString();
23+
}
24+
25+
private Map<Character, Character> getSubstitutionMap(String key) {
26+
final Map<Character, Character> result = new HashMap<>();
27+
char letter = 'a';
28+
for (int index = 0 ; index < key.length() ; index++) {
29+
if (!result.containsKey(key.charAt(index)) && key.charAt(index) != ' ') {
30+
result.put(key.charAt(index), letter++);
31+
}
32+
}
33+
return result;
34+
}
35+
}

0 commit comments

Comments
 (0)