Skip to content

Commit 8514a0f

Browse files
committed
problem: 0219. Contains Duplicate II
1 parent 1dbe049 commit 8514a0f

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

solutions/solution_0219/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 0030. [Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/?envType=study-plan-v2&envId=top-interview-150)
2+
3+
You are given a string `s` and an array of strings `words`. All the strings of `words` are of **the same length**.
4+
5+
A **concatenated string** is a string that exactly contains all the strings of any permutation of `words` concatenated.
6+
7+
- For example, if words` = ["ab","cd","ef"]`, then `"abcdef"`, `"abefcd"`, `"cdabef"`, `"cdefab"`, `"efabcd"`, and `"efcdab"` are all concatenated strings. `"acdbef"` is not a concatenated string because it is not the concatenation of any permutation of `words`.
8+
9+
Return an array of _the starting indices_ of all the concatenated substrings in `s`. You can return the answer in **any order**.
10+
11+
#### **Example 1:**
12+
13+
<pre><code><strong>Input:</strong> s = "barfoothefoobarman", words = ["foo","bar"]
14+
<strong>Output:</strong> [0,9]
15+
<strong>Explanation:</strong>
16+
The substring starting at 0 is "barfoo". It is the concatenation of ["bar","foo"] which is a permutation of words.
17+
The substring starting at 9 is "foobar". It is the concatenation of ["foo","bar"] which is a permutation of words.
18+
</code></pre>
19+
20+
#### **Example 2:**
21+
22+
<pre><code><strong>Input:</strong> s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
23+
<strong>Output:</strong> []
24+
<strong>Explanation:</strong>
25+
There is no concatenated substring.
26+
</code></pre>
27+
28+
#### **Example 3:**
29+
30+
<pre><code><strong>Input:</strong> s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
31+
<strong>Output:</strong> [6,9,12]
32+
<strong>Explanation:</strong>
33+
The substring starting at 6 is "foobarthe". It is the concatenation of ["foo","bar","the"].
34+
The substring starting at 9 is "barthefoo". It is the concatenation of ["bar","the","foo"].
35+
The substring starting at 12 is "thefoobar". It is the concatenation of ["the","foo","bar"].
36+
</code></pre>
37+
38+
#### **Constraints:**
39+
40+
- <code>1 <= s.length <= 10<sup>4</sup></code>
41+
- `1 <= words.length <= 5000`
42+
- `1 <= words[i].length <= 30`
43+
- `s` and `words[i]` consist of lowercase English letters.

solutions/solution_0219/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Solution:
2+
def containsNearbyDuplicate(self, nums: list[int], k: int) -> bool: ...
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from typing import TypedDict
2+
3+
import pytest
4+
5+
from . import Solution
6+
7+
8+
class InputDict(TypedDict):
9+
nums: list[int]
10+
k: int
11+
12+
13+
class CaseDict(TypedDict):
14+
input: InputDict
15+
expected: bool
16+
17+
18+
test_cases: list[CaseDict] = [
19+
{
20+
'input': {
21+
'nums': [1, 2, 3, 1],
22+
'k': 3,
23+
},
24+
'expected': True,
25+
},
26+
{
27+
'input': {
28+
'nums': [1, 0, 1, 1],
29+
'k': 1,
30+
},
31+
'expected': True,
32+
},
33+
{
34+
'input': {
35+
'nums': [1, 2, 3, 1, 2, 3],
36+
'k': 2,
37+
},
38+
'expected': False,
39+
},
40+
]
41+
42+
43+
@pytest.mark.parametrize(
44+
'test_case',
45+
test_cases,
46+
)
47+
def test_solution(test_case: CaseDict):
48+
result = Solution().containsNearbyDuplicate(**test_case['input'])
49+
assert result == test_case['expected']

0 commit comments

Comments
 (0)