Skip to content

Commit 24d1c45

Browse files
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 19.3 MB (40.87%)
1 parent 0cea192 commit 24d1c45

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<p>Given a string <code>s</code> and an array of strings <code>words</code>, determine whether <code>s</code> is a <strong>prefix string</strong> of <code>words</code>.</p>
2+
3+
<p>A string <code>s</code> is a <strong>prefix string</strong> of <code>words</code> if <code>s</code> can be made by concatenating the first <code>k</code> strings in <code>words</code> for some <strong>positive</strong> <code>k</code> no larger than <code>words.length</code>.</p>
4+
5+
<p>Return <code>true</code><em> if </em><code>s</code><em> is a <strong>prefix string</strong> of </em><code>words</code><em>, or </em><code>false</code><em> otherwise</em>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> s = &quot;iloveleetcode&quot;, words = [&quot;i&quot;,&quot;love&quot;,&quot;leetcode&quot;,&quot;apples&quot;]
12+
<strong>Output:</strong> true
13+
<strong>Explanation:</strong>
14+
s can be made by concatenating &quot;i&quot;, &quot;love&quot;, and &quot;leetcode&quot; together.
15+
</pre>
16+
17+
<p><strong class="example">Example 2:</strong></p>
18+
19+
<pre>
20+
<strong>Input:</strong> s = &quot;iloveleetcode&quot;, words = [&quot;apples&quot;,&quot;i&quot;,&quot;love&quot;,&quot;leetcode&quot;]
21+
<strong>Output:</strong> false
22+
<strong>Explanation:</strong>
23+
It is impossible to make s using a prefix of arr.</pre>
24+
25+
<p>&nbsp;</p>
26+
<p><strong>Constraints:</strong></p>
27+
28+
<ul>
29+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
30+
<li><code>1 &lt;= words[i].length &lt;= 20</code></li>
31+
<li><code>1 &lt;= s.length &lt;= 1000</code></li>
32+
<li><code>words[i]</code> and <code>s</code> consist of only lowercase English letters.</li>
33+
</ul>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def isPrefixString(self, s: str, words: List[str]) -> bool:
3+
for word in words:
4+
if len(s) == 0:
5+
return True
6+
if s.startswith(word):
7+
s = s[len(word):]
8+
else:
9+
return False
10+
return len(s) == 0

0 commit comments

Comments
 (0)