All topics
118

Can this text split into dictionary words?

Tier 2Locked

Where it runs

1. The problem

You have one long run of letters with no spaces, like "leetcode", and a dictionary of known words, like ["leet", "code"]. The question is simple: can you cut the run into whole words that are all in the dictionary? You may use the same word as many times as you like. The empty string counts as cut too: it holds zero words.

"leetcode" cuts into "leet" and "code", so the answer is yes. "applepenapple" cuts into "apple", "pen", "apple", with "apple" used twice, so the answer is yes again.

Now "catsandog" with the dictionary ["cats", "dog", "sand", "and", "cat"]. Try a split: "cats", then "and", then "og" is left, and that is not a word. Try "cat", "sand", and "og" is left again. Every cut leaves a tail, so the answer is no. You can't just take the first word that fits. You need to know whether the rest of the string can be cut into words.

This step is part of the paid track

The first 33 steps are open to everyone: lesson, exercise and tests. This one opens with full access. Start with a free account and the free steps.

Create a free accountSee what is included
Splitting a string into dictionary words
wordBreak("catsdog", ["cat", "cats", "dog"])
s
c
a
t
s
d
o
g
ok[i]: T when the first i letters split into words
T
F
F
F
F
F
F
F
wordDict
catcatsdog
piece = -

Can "catsdog" be cut into dictionary words? ok[0] is true: the empty prefix needs no words.

Speed race
Word break of "aaaaaaaaaaaab" with the words a, aa, aaa: plain recursion and a table over prefixes

Bars: how many dictionary checks started at each position of the string.

One operation is one dictionary check. Every frame, each lane does the same number of operations, so the slow one falls behind.

Plain recursion O(2ⁿ)
0 operations
Table over prefixes O(n²)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 20
Plain recursion317K · 3.2 ms
Table over prefixes175 · under 1 ms
n = 40
Plain recursion62.2B · 10 min
Table over prefixes745 · under 1 ms
n = 60
Plain recursion12,220.2T · 3.9 years
Table over prefixes1.7K · under 1 ms

Recognise it

DP on strings (word break)

Say these words and it is this pattern

  • can the string be split into dictionary words
  • segment a string using a word list
  • boolean reachable at each cut position
  • glued text needs word boundaries

Looks similar, isn't

n characters with words up to m give O(nm). A set or a trie keeps the lookup near O(1).

On LeetCode

Problems that cut a string into pieces from a list, or into valid codes, and ask whether it works, in how many ways or at what lowest cost. ok[i] says the first i letters split cleanly, and it comes from any earlier cut j where ok[j] is true and the piece from j to i is valid.

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy