Four-Part Morse Codec and Dictionary Decoding
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
encode: convert every letter oftexts[i]to its Morse pattern. Join adjacent letter patterns with exactly one ASCII space.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.word:texts[i]contains only dots and dashes, with no separators. Return every distinct dictionary word whose concatenated letter patterns equal the entire signal.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 isencode,decode,word, orsentence.- Each
encodetext contains1through200lowercase English letters. - Each
decodetext contains1through200valid alphabet patterns separated by exactly one ASCII space. - Each
wordorsentencetext contains1through60dots or dashes and no other characters. 0 <= dictionary.length <= 100. Every dictionary entry contains1through12lowercase English letters; duplicate entries are allowed and ignored.- Each operation has at most
1000result strings, and the total number of characters across all returned strings is at most10^5. These are input guarantees; every valid result must be returned.