-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathMorseCodeConverter.java
98 lines (91 loc) · 3.27 KB
/
MorseCodeConverter.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.thealgorithms.conversions;
import java.util.HashMap;
import java.util.Map;
/**
* Converts text to Morse code and vice-versa.
* Text to Morse code: Each letter is separated by a space and each word is separated by a pipe (|).
* Example: "HELLO WORLD" -> ".... . .-.. .-.. --- | .-- --- .-. .-.. -.."
*
* Morse code to text: Each letter is separated by a space and each word is separated by a pipe (|).
* Example: ".... . .-.. .-.. --- | .-- --- .-. .-.. -.." -> "HELLO WORLD"
*
* Applications: Used in radio communications and algorithmic challenges.
*
* @author Hardvan
*/
public final class MorseCodeConverter {
private MorseCodeConverter() {
}
private static final Map<Character, String> MORSE_MAP = new HashMap<>();
private static final Map<String, Character> REVERSE_MAP = new HashMap<>();
static {
MORSE_MAP.put('A', ".-");
MORSE_MAP.put('B', "-...");
MORSE_MAP.put('C', "-.-.");
MORSE_MAP.put('D', "-..");
MORSE_MAP.put('E', ".");
MORSE_MAP.put('F', "..-.");
MORSE_MAP.put('G', "--.");
MORSE_MAP.put('H', "....");
MORSE_MAP.put('I', "..");
MORSE_MAP.put('J', ".---");
MORSE_MAP.put('K', "-.-");
MORSE_MAP.put('L', ".-..");
MORSE_MAP.put('M', "--");
MORSE_MAP.put('N', "-.");
MORSE_MAP.put('O', "---");
MORSE_MAP.put('P', ".--.");
MORSE_MAP.put('Q', "--.-");
MORSE_MAP.put('R', ".-.");
MORSE_MAP.put('S', "...");
MORSE_MAP.put('T', "-");
MORSE_MAP.put('U', "..-");
MORSE_MAP.put('V', "...-");
MORSE_MAP.put('W', ".--");
MORSE_MAP.put('X', "-..-");
MORSE_MAP.put('Y', "-.--");
MORSE_MAP.put('Z', "--..");
// Build reverse map for decoding
MORSE_MAP.forEach((k, v) -> REVERSE_MAP.put(v, k));
}
/**
* Converts text to Morse code.
* Each letter is separated by a space and each word is separated by a pipe (|).
*
* @param text The text to convert to Morse code.
* @return The Morse code representation of the text.
*/
public static String textToMorse(String text) {
StringBuilder morse = new StringBuilder();
String[] words = text.toUpperCase().split(" ");
for (int i = 0; i < words.length; i++) {
for (char c : words[i].toCharArray()) {
morse.append(MORSE_MAP.getOrDefault(c, "")).append(" ");
}
if (i < words.length - 1) {
morse.append("| ");
}
}
return morse.toString().trim();
}
/**
* Converts Morse code to text.
* Each letter is separated by a space and each word is separated by a pipe (|).
*
* @param morse The Morse code to convert to text.
* @return The text representation of the Morse code.
*/
public static String morseToText(String morse) {
StringBuilder text = new StringBuilder();
String[] words = morse.split(" \\| ");
for (int i = 0; i < words.length; i++) {
for (String code : words[i].split(" ")) {
text.append(REVERSE_MAP.getOrDefault(code, '?'));
}
if (i < words.length - 1) {
text.append(" ");
}
}
return text.toString();
}
}