You are given a lowercase string s and a dictionary of k known "motifs" (each a non-empty lowercase string). Determine whether s can be split into a sequence of one or more consecutive, non-overlapping pieces, EACH of which is exactly one of the dictionary motifs (a motif may be reused any number of times, and the pieces must cover s exactly with no leftover characters).
Line 1: the string s.
Line 2: an integer k.
Line 3: k space-separated motifs.
Print YES if s can be fully segmented using the given motifs, otherwise print NO.
s and every motif consist only of lowercase English lettersExample 1
Input
lovecode 2 love code
Expected
YES
Explanation
The string splits cleanly into "love" followed by "code", both dictionary motifs: YES.
Example 2
Input
catdog 2 cat dogs
Expected
NO
Explanation
After matching "cat", the remainder "dog" is not in the dictionary (only "dogs" is), so no valid segmentation exists: NO.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →