#1961

Check If String Is a Prefix of Array

pupil · 440 · lc easy +26 · premium · verified · 52.8% accepted · 555 likes · top 43%

Description

You have a string s and an array of strings words. Determine whether s equals the result of concatenating exactly the first k elements of words, for some positive integer k at most words.length.

Return true if such a k exists, otherwise return false.

Example 1:

Input: s = "iloveleetcode", words = ["i","love","leetcode","apples"]
Output: true
Explanation:
s can be made by concatenating "i", "love", and "leetcode" together.

Example 2:

Input: s = "iloveleetcode", words = ["apples","i","love","leetcode"]
Output: false
Explanation:
It is impossible to make s using a prefix of arr.

Code

1
2
3