Skip to content

Commit 9e14bc2

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.9 MB (20.46%)
1 parent 2fba83d commit 9e14bc2

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>You are given an array of strings <code>words</code> and a string <code>pref</code>.</p>
2+
3+
<p>Return <em>the number of strings in </em><code>words</code><em> that contain </em><code>pref</code><em> as a <strong>prefix</strong></em>.</p>
4+
5+
<p>A <strong>prefix</strong> of a string <code>s</code> is any leading contiguous substring of <code>s</code>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> words = [&quot;pay&quot;,&quot;<strong><u>at</u></strong>tention&quot;,&quot;practice&quot;,&quot;<u><strong>at</strong></u>tend&quot;], <code>pref </code>= &quot;at&quot;
12+
<strong>Output:</strong> 2
13+
<strong>Explanation:</strong> The 2 strings that contain &quot;at&quot; as a prefix are: &quot;<u><strong>at</strong></u>tention&quot; and &quot;<u><strong>at</strong></u>tend&quot;.
14+
</pre>
15+
16+
<p><strong class="example">Example 2:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> words = [&quot;leetcode&quot;,&quot;win&quot;,&quot;loops&quot;,&quot;success&quot;], <code>pref </code>= &quot;code&quot;
20+
<strong>Output:</strong> 0
21+
<strong>Explanation:</strong> There are no strings that contain &quot;code&quot; as a prefix.
22+
</pre>
23+
24+
<p>&nbsp;</p>
25+
<p><strong>Constraints:</strong></p>
26+
27+
<ul>
28+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
29+
<li><code>1 &lt;= words[i].length, pref.length &lt;= 100</code></li>
30+
<li><code>words[i]</code> and <code>pref</code> consist of lowercase English letters.</li>
31+
</ul>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Approach 2: Built-in Methods
2+
3+
# n = length of input array, m = length of the prefix string
4+
# Time: O(m * n)
5+
# Space: O(1)
6+
7+
class Solution:
8+
def prefixCount(self, words: List[str], pref: str) -> int:
9+
return sum(word.startswith(pref) for word in words)
10+

0 commit comments

Comments
 (0)