FastPrepDelimited Morse Decoding and Word Segmentation

Delimited Morse Decoding and Word Segmentation

Google logoGoogle● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

Decode an uppercase-English Morse message that uses the literal string xxx between encoded letters.

  • When sharedDelimiter is false, xxxxx separates words. Return the single decoded lowercase sentence.
  • When sharedDelimiter is true, every separator is xxx. Decode the continuous lowercase letter string, then return every sentence obtainable by splitting it into words from dictionary.

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 1 through 100.
  • At most 10000 valid sentences exist.

More Google problems

See Google hiring insights
public String[] decodeDelimitedMorse(String encoded, String[] dictionary, boolean sharedDelimiter) {
    // Write your code here.
}
encoded".-xxx-..xxxxx..-"
dictionary[]
sharedDelimiterfalse
expected["ad u"]
Checking account…