LogiQuest

237. Word Break

MediumDynamic Programming

Print YES if s can be split into a sequence of dictionary words (words may be reused), else NO.

Input format

Line 1: s Line 2: k Line 3: k dictionary words

Output format

YES or NO

Example 1

Input

leetcode
2
leet code

Output

YES

Example 2

Input

applepenapple
2
apple pen

Output

YES

Constraints

1 ≤ |s| ≤ 300 1 ≤ k ≤ 1000

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…