Skip to content

Commit 6639928

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (28.54%), Memory - 17.7 MB (28.23%)
1 parent 363ad11 commit 6639928

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<p>Given an array of string <code>words</code>, return <em>all strings in </em><code>words</code><em> that is a <strong>substring</strong> of another word</em>. You can return the answer in <strong>any order</strong>.</p>
2+
3+
<p>A <strong>substring</strong> is a contiguous sequence of characters within a string</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> words = [&quot;mass&quot;,&quot;as&quot;,&quot;hero&quot;,&quot;superhero&quot;]
10+
<strong>Output:</strong> [&quot;as&quot;,&quot;hero&quot;]
11+
<strong>Explanation:</strong> &quot;as&quot; is substring of &quot;mass&quot; and &quot;hero&quot; is substring of &quot;superhero&quot;.
12+
[&quot;hero&quot;,&quot;as&quot;] is also a valid answer.
13+
</pre>
14+
15+
<p><strong class="example">Example 2:</strong></p>
16+
17+
<pre>
18+
<strong>Input:</strong> words = [&quot;leetcode&quot;,&quot;et&quot;,&quot;code&quot;]
19+
<strong>Output:</strong> [&quot;et&quot;,&quot;code&quot;]
20+
<strong>Explanation:</strong> &quot;et&quot;, &quot;code&quot; are substring of &quot;leetcode&quot;.
21+
</pre>
22+
23+
<p><strong class="example">Example 3:</strong></p>
24+
25+
<pre>
26+
<strong>Input:</strong> words = [&quot;blue&quot;,&quot;green&quot;,&quot;bu&quot;]
27+
<strong>Output:</strong> []
28+
<strong>Explanation:</strong> No string of words is substring of another string.
29+
</pre>
30+
31+
<p>&nbsp;</p>
32+
<p><strong>Constraints:</strong></p>
33+
34+
<ul>
35+
<li><code>1 &lt;= words.length &lt;= 100</code></li>
36+
<li><code>1 &lt;= words[i].length &lt;= 30</code></li>
37+
<li><code>words[i]</code> contains only lowercase English letters.</li>
38+
<li>All the strings of <code>words</code> are <strong>unique</strong>.</li>
39+
</ul>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Approach 1: Brute Force
2+
3+
# n = size of `words` array, m = length of longest string in `words`
4+
# Time: O(m * n^2)
5+
# Space: O(m * n)
6+
7+
class Solution:
8+
def stringMatching(self, words: List[str]) -> List[str]:
9+
matching_words = []
10+
11+
for curr_word_idx in range(len(words)):
12+
for other_word_idx in range(len(words)):
13+
if curr_word_idx == other_word_idx:
14+
continue
15+
if words[curr_word_idx] in words[other_word_idx]:
16+
matching_words.append(words[curr_word_idx])
17+
break
18+
19+
return matching_words
20+

0 commit comments

Comments
 (0)