|
| 1 | +package com.thealgorithms.conversions; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +/** |
| 7 | + * Converts text to Morse code and vice-versa. |
| 8 | + * Text to Morse code: Each letter is separated by a space and each word is separated by a pipe (|). |
| 9 | + * Example: "HELLO WORLD" -> ".... . .-.. .-.. --- | .-- --- .-. .-.. -.." |
| 10 | + * |
| 11 | + * Morse code to text: Each letter is separated by a space and each word is separated by a pipe (|). |
| 12 | + * Example: ".... . .-.. .-.. --- | .-- --- .-. .-.. -.." -> "HELLO WORLD" |
| 13 | + * |
| 14 | + * Applications: Used in radio communications and algorithmic challenges. |
| 15 | + * |
| 16 | + * @author Hardvan |
| 17 | + */ |
| 18 | +public final class MorseCodeConverter { |
| 19 | + private MorseCodeConverter() { |
| 20 | + } |
| 21 | + |
| 22 | + private static final Map<Character, String> MORSE_MAP = new HashMap<>(); |
| 23 | + private static final Map<String, Character> REVERSE_MAP = new HashMap<>(); |
| 24 | + |
| 25 | + static { |
| 26 | + MORSE_MAP.put('A', ".-"); |
| 27 | + MORSE_MAP.put('B', "-..."); |
| 28 | + MORSE_MAP.put('C', "-.-."); |
| 29 | + MORSE_MAP.put('D', "-.."); |
| 30 | + MORSE_MAP.put('E', "."); |
| 31 | + MORSE_MAP.put('F', "..-."); |
| 32 | + MORSE_MAP.put('G', "--."); |
| 33 | + MORSE_MAP.put('H', "...."); |
| 34 | + MORSE_MAP.put('I', ".."); |
| 35 | + MORSE_MAP.put('J', ".---"); |
| 36 | + MORSE_MAP.put('K', "-.-"); |
| 37 | + MORSE_MAP.put('L', ".-.."); |
| 38 | + MORSE_MAP.put('M', "--"); |
| 39 | + MORSE_MAP.put('N', "-."); |
| 40 | + MORSE_MAP.put('O', "---"); |
| 41 | + MORSE_MAP.put('P', ".--."); |
| 42 | + MORSE_MAP.put('Q', "--.-"); |
| 43 | + MORSE_MAP.put('R', ".-."); |
| 44 | + MORSE_MAP.put('S', "..."); |
| 45 | + MORSE_MAP.put('T', "-"); |
| 46 | + MORSE_MAP.put('U', "..-"); |
| 47 | + MORSE_MAP.put('V', "...-"); |
| 48 | + MORSE_MAP.put('W', ".--"); |
| 49 | + MORSE_MAP.put('X', "-..-"); |
| 50 | + MORSE_MAP.put('Y', "-.--"); |
| 51 | + MORSE_MAP.put('Z', "--.."); |
| 52 | + |
| 53 | + // Build reverse map for decoding |
| 54 | + MORSE_MAP.forEach((k, v) -> REVERSE_MAP.put(v, k)); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Converts text to Morse code. |
| 59 | + * Each letter is separated by a space and each word is separated by a pipe (|). |
| 60 | + * |
| 61 | + * @param text The text to convert to Morse code. |
| 62 | + * @return The Morse code representation of the text. |
| 63 | + */ |
| 64 | + public static String textToMorse(String text) { |
| 65 | + StringBuilder morse = new StringBuilder(); |
| 66 | + String[] words = text.toUpperCase().split(" "); |
| 67 | + for (int i = 0; i < words.length; i++) { |
| 68 | + for (char c : words[i].toCharArray()) { |
| 69 | + morse.append(MORSE_MAP.getOrDefault(c, "")).append(" "); |
| 70 | + } |
| 71 | + if (i < words.length - 1) { |
| 72 | + morse.append("| "); |
| 73 | + } |
| 74 | + } |
| 75 | + return morse.toString().trim(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Converts Morse code to text. |
| 80 | + * Each letter is separated by a space and each word is separated by a pipe (|). |
| 81 | + * |
| 82 | + * @param morse The Morse code to convert to text. |
| 83 | + * @return The text representation of the Morse code. |
| 84 | + */ |
| 85 | + public static String morseToText(String morse) { |
| 86 | + StringBuilder text = new StringBuilder(); |
| 87 | + String[] words = morse.split(" \\| "); |
| 88 | + for (int i = 0; i < words.length; i++) { |
| 89 | + for (String code : words[i].split(" ")) { |
| 90 | + text.append(REVERSE_MAP.getOrDefault(code, '?')); |
| 91 | + } |
| 92 | + if (i < words.length - 1) { |
| 93 | + text.append(" "); |
| 94 | + } |
| 95 | + } |
| 96 | + return text.toString(); |
| 97 | + } |
| 98 | +} |
0 commit comments