Morse Code Translator - Problem
Build a bidirectional Morse code translator that can both encode text to Morse code and decode Morse code back to text.
Your translator should:
- Encode: Convert alphabetic characters (A-Z, case insensitive) to Morse code
- Decode: Convert Morse code back to uppercase letters
- Handle spaces: Single space between Morse letters, double space between words
- Ignore: Non-alphabetic characters during encoding
Use the standard International Morse Code mapping where A = '.-', B = '-...', etc.
Input & Output
Example 1 — Encoding Text to Morse
$
Input:
text = "HELLO WORLD", mode = "encode"
›
Output:
.... . .-.. .-.. --- .-- --- .-. .-.. -..
💡 Note:
Each letter is converted to its Morse code: H=...., E=., L=.-.., L=.-.., O=---. Single spaces separate letters, double spaces separate words.
Example 2 — Decoding Morse to Text
$
Input:
text = ".... . .-.. .--.", mode = "decode"
›
Output:
HELP
💡 Note:
Each Morse pattern is converted back: ....=H, .=E, .-..=L, .--.=P
Example 3 — Multiple Words
$
Input:
text = "... --- ... -- --- .-. ... .", mode = "decode"
›
Output:
SOS MORSE
💡 Note:
Double spaces indicate word boundaries. First word: S=..., O=---, S=... gives 'SOS'. Second word gives 'MORSE'.
Constraints
- 1 ≤ text.length ≤ 1000
- text contains only letters, spaces, dots, and dashes
- mode is either "encode" or "decode"
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code