English to Morse Code
Updated
Problem statement
Given a string text made of English words separated by single spaces, return text written in Morse code.
Write each letter with its code from the table below. Each code is made of the characters . (dot) and - (dash). Uppercase and lowercase letters use the same code, so A and a are both .-.
- Inside a word, separate the codes of consecutive letters with one space.
- Between two words, write a space, a slash and a space:
" / ".
The result does not start or end with a space.
| Letter | Code | Letter | Code |
|---|---|---|---|
a | .- | n | -. |
b | -... | o | --- |
c | -.-. | p | .--. |
d | -.. | q | --.- |
e | . | r | .-. |
f | ..-. | s | ... |
g | --. | t | - |
h | .... | u | ..- |
i | .. | v | ...- |
j | .--- | w | .-- |
k | -.- | x | -..- |
l | .-.. | y | -.-- |
m | -- | z | --.. |
Function
toMorse(text: String) → StringExamples
Example 1
text = "SOS"return = "... --- ..."S is ... and O is ---. The three letters form one word, so their codes are separated by single spaces: "... --- ...".
Example 2
text = "Hello World"return = ".... . .-.. .-.. --- / .-- --- .-. .-.. -.."Hello becomes .... . .-.. .-.. --- and World becomes .-- --- .-. .-.. -... The two words are joined with " / ". Uppercase H and W use the same codes as h and w.
Example 3
text = "I am"return = ".. / .- --"I is a word of one letter, ... am becomes .- --. Joining the two words gives ".. / .- --".
Constraints
1 <= text.length <= 1000textconsists of English letters (atozandAtoZ) and spaces.textdoes not start or end with a space, and no two spaces are next to each other.
More Oscar Health interview questions
Similar questions at other companies
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Run-Length Encoding and DecodingAirbnb · PHONE SCREENAirbnb interview questions
- Straight-Line Word SearchReddit · PHONE SCREENReddit interview questions
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Valid ParenthesesByteDance · PHONE SCREENByteDance interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Check if a String Is a PalindromeCisco · PHONE SCREENCisco interview questions
- Halfway Course in a Single TrackCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions