Splitting a hashtag into words

Write code · Challenge · §10.2 Filling a one-dimensional table

Hashtags join words without spaces, and tag does not include the # sign. Complete the function solution, which returns the number of ways to cut the hashtag tag into pieces so that every piece is a word in the word list words and no letters are left over from the start to the end.

The same word may be used more than once. Two ways are different if they cut at different positions. A word listed more than once in words still counts as one word. If the whole tag is one word, not cutting it is also one way. If tag cannot be cut this way, return 0.

Function

solution(tag, words)

Constraints

Examples

tagwordsresult
'dailylook'['dai', 'ly', 'daily', 'lo', 'ok', 'look']4
'catvlog'['cat', 'vlog', 'ca', 'tv', 'log']2

Starter code

def solution(tag, words):
    answer = 0
    return answer

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.