A chain of relay towers transmits text one after another: tower i sends a fixed chunk words[i], and each tower's transmission begins the instant the previous one ends, with no gaps or separators between chunks. A monitoring station recorded a transcript s, believed to start exactly at the beginning of the first tower's transmission and to stop exactly at some tower's boundary.
Given the recorded transcript and the ordered list of tower chunks, determine whether s is exactly equal to the concatenation of the first k chunks for some k between 1 and the number of towers (inclusive), with no partial chunk included and no leftover text in s.
s.n, the number of towers.n space-separated strings, words[0] through words[n-1], in transmission order.Print true if s equals the concatenation of some prefix of words, and false otherwise.
words[i] <= 20s <= 1000s and every words[i] consist only of lowercase English letters.Example 1
Input
codesprint 2 code sprint
Expected
true
Explanation
Concatenating both chunks in order gives "code" + "sprint" = "codesprint", which is exactly equal to s, so the answer is true.
Example 2
Input
codesprints 2 code sprint
Expected
false
Explanation
The only prefixes of words are "code" (length 4) and "code"+"sprint" = "codesprint" (length 10). Neither equals s = "codesprints" (length 11), so the answer is false.
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 →