Skip to content

Commit 2436a75

Browse files
committed
Sync LeetCode submission Runtime - 7 ms (89.37%), Memory - 17.7 MB (34.51%)
1 parent 6639928 commit 2436a75

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>You are given a <strong>0-indexed</strong> string array <code>words</code>.</p>
2+
3+
<p>Let&#39;s define a <strong>boolean</strong> function <code>isPrefixAndSuffix</code> that takes two strings, <code>str1</code> and <code>str2</code>:</p>
4+
5+
<ul>
6+
<li><code>isPrefixAndSuffix(str1, str2)</code> returns <code>true</code> if <code>str1</code> is <strong>both</strong> a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of <code>str2</code>, and <code>false</code> otherwise.</li>
7+
</ul>
8+
9+
<p>For example, <code>isPrefixAndSuffix(&quot;aba&quot;, &quot;ababa&quot;)</code> is <code>true</code> because <code>&quot;aba&quot;</code> is a prefix of <code>&quot;ababa&quot;</code> and also a suffix, but <code>isPrefixAndSuffix(&quot;abc&quot;, &quot;abcd&quot;)</code> is <code>false</code>.</p>
10+
11+
<p>Return <em>an integer denoting the <strong>number</strong> of index pairs </em><code>(i, j)</code><em> such that </em><code>i &lt; j</code><em>, and </em><code>isPrefixAndSuffix(words[i], words[j])</code><em> is </em><code>true</code><em>.</em></p>
12+
13+
<p>&nbsp;</p>
14+
<p><strong class="example">Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> words = [&quot;a&quot;,&quot;aba&quot;,&quot;ababa&quot;,&quot;aa&quot;]
18+
<strong>Output:</strong> 4
19+
<strong>Explanation:</strong> In this example, the counted index pairs are:
20+
i = 0 and j = 1 because isPrefixAndSuffix(&quot;a&quot;, &quot;aba&quot;) is true.
21+
i = 0 and j = 2 because isPrefixAndSuffix(&quot;a&quot;, &quot;ababa&quot;) is true.
22+
i = 0 and j = 3 because isPrefixAndSuffix(&quot;a&quot;, &quot;aa&quot;) is true.
23+
i = 1 and j = 2 because isPrefixAndSuffix(&quot;aba&quot;, &quot;ababa&quot;) is true.
24+
Therefore, the answer is 4.</pre>
25+
26+
<p><strong class="example">Example 2:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> words = [&quot;pa&quot;,&quot;papa&quot;,&quot;ma&quot;,&quot;mama&quot;]
30+
<strong>Output:</strong> 2
31+
<strong>Explanation:</strong> In this example, the counted index pairs are:
32+
i = 0 and j = 1 because isPrefixAndSuffix(&quot;pa&quot;, &quot;papa&quot;) is true.
33+
i = 2 and j = 3 because isPrefixAndSuffix(&quot;ma&quot;, &quot;mama&quot;) is true.
34+
Therefore, the answer is 2. </pre>
35+
36+
<p><strong class="example">Example 3:</strong></p>
37+
38+
<pre>
39+
<strong>Input:</strong> words = [&quot;abab&quot;,&quot;ab&quot;]
40+
<strong>Output:</strong> 0
41+
<strong>Explanation: </strong>In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix(&quot;abab&quot;, &quot;ab&quot;) is false.
42+
Therefore, the answer is 0.</pre>
43+
44+
<p>&nbsp;</p>
45+
<p><strong>Constraints:</strong></p>
46+
47+
<ul>
48+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
49+
<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
50+
<li><code>words[i]</code> consists only of lowercase English letters.</li>
51+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Approach 1: Brute Force
2+
3+
# n = number of words in input array, m = average length of words
4+
# Time: O(n^2 * m)
5+
# Space: O(1)
6+
7+
class Solution:
8+
def countPrefixSuffixPairs(self, words: List[str]) -> int:
9+
n = len(words)
10+
count = 0
11+
12+
for i in range(n):
13+
for j in range(i + 1, n):
14+
str1 = words[i]
15+
str2 = words[j]
16+
17+
if len(str1) > len(str2):
18+
continue
19+
20+
if str2.startswith(str1) and str2.endswith(str1):
21+
count += 1
22+
23+
return count
24+

0 commit comments

Comments
 (0)