FastPrepFour-Part Morse Codec and Dictionary Decoding

Four-Part Morse Codec and Dictionary Decoding

Amazon logoAmazon● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement a Morse codec with four operations. You are given equally sized arrays operations and texts, plus a shared array dictionary of non-empty lowercase English words.

Return one string array for each operation, preserving operation order. A basic encoding or decoding produces a one-element array. Dictionary operations return all distinct matching words or sentences in lexicographic order, or an empty array when none exist.

Morse alphabet

Use the following fixed mapping for lowercase letters. A dot and a dash are the literal characters . and -.

a: .-  b: -...  c: -.-.  d: -..  e: .
f: ..-.  g: --.  h: ....  i: ..  j: .---
k: -.-  l: .-..  m: --  n: -.  o: ---
p: .--.  q: --.-  r: .-.  s: ...  t: -
u: ..-  v: ...-  w: .--  x: -..-  y: -.--
z: --..

Four operations

  1. encode: convert every letter of texts[i] to its Morse pattern. Join adjacent letter patterns with exactly one ASCII space.
  2. decode: texts[i] contains valid letter patterns separated by exactly one ASCII space, with no leading or trailing space. Decode those tokens and concatenate their letters.
  3. word: texts[i] contains only dots and dashes, with no separators. Return every distinct dictionary word whose concatenated letter patterns equal the entire signal.
  4. sentence: texts[i] contains only dots and dashes, with no separators. Return every sequence of one or more dictionary words whose concatenated Morse patterns equal the entire signal. A word may be reused. Render a sequence with exactly one ASCII space between its words.

For dictionary matching, concatenate the patterns within each word without spaces. Different words can share that pattern: retain every distinct word. Repeated dictionary entries do not create repeated results. Include both a one-word sentence and any valid longer sequences; enumerate all results without truncation.

All inputs satisfy the rules below. Basic encode and decode operations use the fixed alphabet regardless of the dictionary.

Function

processMorse(operations: String[], texts: String[], dictionary: String[]) → String[][]

Examples

Example 1

operations = ["encode","decode","word","sentence"]texts = ["sos","... --- ...","...","..."]dictionary = ["s","e","ie"]return = [["... --- ..."],["sos"],["ie","s"],["e e e","ie","s"]]

Encoding and separated decoding are inverses. Both ie and s encode to ... without separators. Sentence decoding also includes e e e; the word e may be reused.

Example 2

operations = ["word","sentence"]texts = ["-...","-..."]dictionary = ["b","de","d","e","ni","n","i","b"]return = [["b","de","ni"],["b","d e","de","n e e","n i","ni"]]

The distinct dictionary words b, de, and ni share the whole pattern. Sentence mode additionally permits boundaries after d or n. The repeated dictionary entry b creates no duplicate result.

Example 3

operations = ["word","sentence","encode","decode"]texts = ["----","----","az",".- --.."]dictionary = ["e","a","a"]return = [[],[],[".- --.."],["az"]]

No supplied dictionary word can begin the dash-only signal, so both unseparated modes return an empty array. Basic encoding and decoding use the alphabet independently of the dictionary.

Constraints

  • 1 <= operations.length = texts.length <= 20. Every operation is encode, decode, word, or sentence.
  • Each encode text contains 1 through 200 lowercase English letters.
  • Each decode text contains 1 through 200 valid alphabet patterns separated by exactly one ASCII space.
  • Each word or sentence text contains 1 through 60 dots or dashes and no other characters.
  • 0 <= dictionary.length <= 100. Every dictionary entry contains 1 through 12 lowercase English letters; duplicate entries are allowed and ignored.
  • Each operation has at most 1000 result strings, and the total number of characters across all returned strings is at most 10^5. These are input guarantees; every valid result must be returned.

More Amazon problems

See Amazon hiring insights
public String[][] processMorse(String[] operations, String[] texts, String[] dictionary) {
    // Write your code here.
}
operations["encode","decode","word","sentence"]
texts["sos","... --- ...","...","..."]
dictionary["s","e","ie"]
expected[["... --- ..."], ["sos"], ["ie", "s"], ["e e e", "ie", "s"]]
Checking account…