BugForge
DSA

Split a Search Query Into Keywords

Mediumdynamic programmingstrings 00:00

Return true if the query s (no spaces) can be split into a sequence of words from wordList (words reusable).

Signature

canSplit(s: string, wordList: string[]) → bool
Example 1
s = "smartspeaker"
wordList = ["smart","speaker","speak"]
output = true
Example 2
s = "catsandog"
wordList = ["cats","dog","sand","and","cat"]
output = false

Constraints

  • 1 ≤ |s| ≤ 300
  • 1 ≤ |wordList| ≤ 1000
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).