Skip to the code
cf.
+50 XP

#568 Count Hidden Copies of a Keyword

hard

A spam filter looks for a keyword hidden inside a message by spreading its letters out. A hidden copy is a choice of positions in text, in increasing order, whose letters spell keyword exactly. Two copies are different if they use a different set of positions.

Write countHiddenCopies(text, keyword) that returns the number of hidden copies, modulo 1000000007.

Example 1

Input: text = "aab", keyword = "ab"
Output: 2

The b can follow the first a or the second a.

Example 2

Input: text = "abc", keyword = "d"
Output: 0

The keyword's letter never appears.

Constraints

  • 1 <= text.length, keyword.length <= 1000
  • Both contain only lowercase English 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).