This problem requires segmenting a continuous stream of characters into meaningful English words using a given dictionary, or report that such a segmentation does not exist.
Such programs are often required in text recognition systems where they are used to decode and validate continuous input from OCR (Optical Character Recognition) software or from speech-to-text systems to produce accurate and meaningful text.
Given a continuous stream of characters, determine if the stream can be segmented into a sentence consisting of valid English words. The sentence does not have to make grammatical sense. It simply needs to be a sequence of valid words according to a dictionary. A dictionary of valid words is provided for reference.
stream
consisting of lowercase letters (no spaces or special characters).It is a good idea to solve this problem in two separate stages.
Write a function that returns:
Write a function that returns:
decoded
of a space-delimited sentence of valid
words if
the stream can be segmented into valid English words (according to the dictionary).decoded
which is empty if
the stream cannot be segmented into valid English words (according to the dictionary).validWord
(available in the code below) operates in constant time.Some examples of inputs and outputs are as follows:
Example 1:
stream = "thisisasimplevalidstring"
true
, decoded = "this is a simple valid string"
Example 2:
stream = "uuydhllhfis"
false
, decoded = ""
Example 3:
stream = "thisisanalmostavalidbutinvavidstring"
false
, decoded = ""
Example 4:
stream = "thequickbrownfoxjumpsoverthelazydog"
true
, decoded = "the quick brown fox jumps over the lazy dog"
The following code contains an empty function (to be implemented by you), and a main that tests it.