Ctrl K

Glued from the First Words

Easy
You are given a string phrase and a list of words words. Check whether phrase can be produced by gluing together, with no spaces, the **first** k words of the list (in order) for some k from 1 to len(words). Return True if it can, and False otherwise.
Example 1
phrase="sunnyday", words=["sun", "ny", "day", "off"] → True

"sun" + "ny" + "day" = "sunnyday", using the first three words.

Example 2
phrase="sunnyday", words=["day", "sun", "ny"] → False

Gluing must start at the front of the list, and the first word is "day".

Example 3
phrase="tea", words=["te", "ab"] → False

"te" is too short and "teab" is already too long, so exactly "tea" never appears.

Constraints
  • 1 ≤ len(words) ≤ 100
  • 1 ≤ len(words[i]) ≤ 20
  • 1 ≤ len(phrase) ≤ 1000
  • phrase and the words consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsDistance to the Nearest Marker EasyRise and Fall Lineup EasyFirst Mirror Word EasyA Subsequence of One Word Only MediumLongest Word by Crossing Out Medium
loading Python…
Input
phrase="sunnyday", words=["sun", "ny", "day", "off"]
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 127 tests on the server, hidden ones included.