Squashed Hashtag
A screen reader reads posts aloud. A hashtag like #lemoncakerecipe is one long run of letters, so the reader tries to cut it into words from a known list and say "lemon cake recipe".
Before it can read the hashtag, it first needs to know whether any clean cut exists.
Your task
Write canSplit(tag, words). Return true if tag can be cut into pieces that are all in words, and false otherwise.
Every letter belongs to exactly one piece, and pieces keep their order. A word can be used any number of times. The tag has no # and is all lowercase.
canSplit(tag: string, words: string[]) → bool
Examples
tag = "lemoncakerecipe" words = ["cake", "lemon", "recipe", "rec", "ipe"]
true
Cut it as lemon | cake | recipe. The cut lemon | cake | rec | ipe also works, but one clean cut is enough.
tag = "startupweek" words = ["star", "start", "up", "week"]
true
The cut start | up | week works. If you grab the shortest matching word first, you take "star" and are left with "tupweek", which cannot be cut into words. So you must be ready to try a different first word.
tag = "nightowls" words = ["night", "owl", "nigh", "towel"]
false
The cut night | owl leaves a lone "s" at the end. Starting with nigh leaves "towls", and "towel" does not match it. No cut covers every letter.
Constraints
- 1 ≤ tag.length ≤ 300
- 1 ≤ words.length ≤ 1,000
- 1 ≤ words[i].length ≤ 20, all words different
- tag and words contain only lowercase letters a to z.
Start with brute force
Try every word that matches the start of the tag, recurse on the rest, and backtrack when stuck.
On forty as followed by a b, with words a, aa, aaa, it tries billions of splits of the as before learning the b can never be covered. It asks "can the rest from position 20 be cut?" over and over, and the answer is always the same.
Hints
0 of 4 shown- Hint 1
Suppose you know that the first 7 letters of
startupweek, which arestartup, can be cut cleanly. What is left to check? - Hint 2
The rest,
week, must be one word. In general, the firstiletters can be cut if, for some earlierj, the firstjletters can be cut and lettersjtoiform one word. - Hint 3
Keep an array
okwhereok[i]says whether the firstiletters can be cut.ok[0]is true, since an empty start needs no words. Fill the array from left to right. - Hint 4
For each
i, try last-word lengths 1 to 20 (no word is longer). Ifok[i - len]is true andtag.slice(i - len, i)is in a set of the words, markok[i]true. Returnok[tag.length].
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Every cut has a last word. If it covers letters j to i, the first j letters must be cuttable on their own. So ok[i] is true when some ok[j] is true and tag[j..i) is a word.
Trace startupweek with words star, start, up, week. Positions are letter counts:
ok[0]is true, because the empty start needs no words.ok[4]is true, because "star" is a word andok[0]is true.ok[5]is true, because "start" is a word andok[0]is true.ok[6]is false. The earlier true positions are 0, 4 and 5, and from them the last word would be "startu", "tu" or "u", and none of those is a word.ok[7]is true, because "up" followsok[5]. Fromok[4]the last word would be "tup", which is not a word.ok[11]is true, because "week" followsok[7], so the answer is true.
The table keeps every reachable cut point at once, so the dead end after "star" costs nothing, because the cut after "start" is already recorded.
function canSplit(tag, words) {
const known = new Set(words);
let longest = 0;
for (const w of words) longest = Math.max(longest, w.length);
// ok[i] = can the first i letters be cut into known words?
const ok = new Array(tag.length + 1).fill(false);
ok[0] = true;
for (let i = 1; i <= tag.length; i++) {
for (let len = 1; len <= longest && len <= i && !ok[i]; len++) {
// the last word is tag[i - len .. i)
if (ok[i - len] && known.has(tag.slice(i - len, i))) ok[i] = true;
}
}
return ok[tag.length];
}def can_split(tag, words):
known = set(words)
longest = max(len(w) for w in words)
# ok[i] = can the first i letters be cut into known words?
ok = [True] + [False] * len(tag)
for i in range(1, len(tag) + 1):
for length in range(1, min(longest, i) + 1):
# the last word is tag[i - length : i]
if ok[i - length] and tag[i - length:i] in known:
ok[i] = True
break
return ok[len(tag)]Run your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.