All problems

Problem 192

Word Break

Medium · 20 pts · String, Dynamic Programming, Hash Table

Problem

You are given a string S and a dictionary of words. Decide whether S can be split into a sequence of one or more dictionary words, written one after another with nothing left over. The same dictionary word may be used several times. Let ok[i] say whether the first i characters can be split. ok[0] is true, and ok[i] is true when some dictionary word ends at position i and ok[i - length of that word] is true. The answer is ok[|S|].

Input

First line: string S. Second line: integer N. Third line: N dictionary words.

Output

Print YES or NO.

Constraints

1 <= |S| <= 300; 1 <= N <= 1000; 1 <= word length <= 20; lowercase letters

Example input

leetcode
2
leet code

Example output

YES

Explanation

"leetcode" can be split into "leet" followed by "code", and both are in the dictionary, so the answer is YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Path Sum Next: Partition Equal Subset Sum