Delimited Morse Decoding and Word Segmentation
Problem statement
Decode an uppercase-English Morse message that uses the literal string xxx between encoded letters.
- When
sharedDelimiteris false,xxxxxseparates words. Return the single decoded lowercase sentence. - When
sharedDelimiteris true, every separator isxxx. Decode the continuous lowercase letter string, then return every sentence obtainable by splitting it into words fromdictionary.
Return sentences in lexicographic order. Dictionary words are unique lowercase strings. The encoded input is valid Morse for letters A–Z. Empty input decodes to one empty sentence.
Function
decodeDelimitedMorse(encoded: String, dictionary: String[], sharedDelimiter: boolean) → String[]Examples
Example 1
encoded = ".-xxx-..xxxxx..-"dictionary = []sharedDelimiter = falsereturn = ["ad u"].-, -.., and ..- decode to A, D, and U; five x characters separate the two words.
Example 2
encoded = ".-xxx-..xxx..-"dictionary = ["ad","u","a","du"]sharedDelimiter = truereturn = ["a du","ad u"]The decoded letters are adu, which have two valid dictionary segmentations.
Example 3
encoded = ""dictionary = []sharedDelimiter = falsereturn = [""]The empty message contains one empty sentence.
Constraints
0 <= encoded.length <= 2000.0 <= dictionary.length <= 5000.- Dictionary words are unique lowercase English strings with length from
1through100. - At most
10000valid sentences exist.