Skip to the code
cf.
+25 XP

#548 Count Search Query Segmentations

medium

A search box receives queries typed without spaces, such as searchbox. To suggest interpretations, it counts every way to cut query into words from the list words, where each word may be used any number of times.

Write countSegmentations(query, words) that returns the number of different ways to cut the query so that every piece is a word, modulo 1000000007. Two ways are different if they cut at different positions.

Example 1

Input: query = "searchbox", words = ["search","box","sear","ch","s","earch"]
Output: 3

search + box, sear + ch + box, and s + earch + box.

Example 2

Input: query = "abc", words = ["ab"]
Output: 0

After ab, the c is not a word.

Constraints

  • 1 <= query.length <= 1000
  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 20, all different
  • All strings are lowercase letters.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).