Skip to content

Commit bfd394b

Browse files
committed
D. J.:
- Added the leetcode problem and solution for 30 and 76
1 parent ccc2163 commit bfd394b

5 files changed

+134
-0
lines changed

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
4141
- [26 Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/)
4242
- [27 Remove Element](https://leetcode.com/problems/remove-element/description/)
4343
- [28 Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/)
44+
- [30 Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/description/)
4445
- [42 Trapping Rain Water](https://leetcode.com/problems/trapping-rain-water/description/)
4546
- [45 Jump Game II](https://leetcode.com/problems/jump-game-ii/description/)
4647
- [49 Group Anagrams](https://leetcode.com/problems/group-anagrams/description/)
@@ -55,6 +56,7 @@ This repository contains awesome LeetCode problems and solutions written in Pyth
5556
- [68 Text Justification](https://leetcode.com/problems/text-justification/description/)
5657
- [69 Sqrt(x)](https://leetcode.com/problems/sqrtx/description/)
5758
- [71 Simplify Path](https://leetcode.com/problems/simplify-path/description/)
59+
- [76 Minimum Window Substring](https://leetcode.com/problems/minimum-window-substring/description/)
5860
- [80 Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description/)
5961
- [82 Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/)
6062
- [86 Partition List](https://leetcode.com/problems/partition-list/description/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import collections
2+
from typing import List
3+
4+
5+
class Solution:
6+
"""Base class for all LeetCode Problems."""
7+
8+
def findSubstring(self, s: str, words: List[str]) -> List[int]:
9+
"""
10+
You are given a string s and an array of strings words. All the strings of
11+
words are of the same length.
12+
13+
A concatenated string is a string that exactly contains all the strings of any
14+
permutation of words concatenated.
15+
16+
- For example, if words = ["ab","cd","ef"], then "abcdef", "abefcd", "cdabef",
17+
"cdefab", "efabcd", and "efcdab" are all concatenated strings. "acdbef" is not
18+
a concatenated string because it is not the concatenation of any permutation
19+
of words.
20+
21+
Return an array of the starting indices of all the concatenated substrings in s.
22+
You can return the answer in any order.
23+
"""
24+
needCount = collections.defaultdict(int)
25+
res = []
26+
27+
for word in words:
28+
needCount[word] += 1
29+
30+
wordLen = len(words[0])
31+
windowLen = len(words) * wordLen
32+
for left in range(len(s) - windowLen + 1):
33+
haveCount = collections.defaultdict(int)
34+
right = left
35+
36+
while right < left + windowLen:
37+
word = s[right : right + wordLen]
38+
39+
if word not in needCount:
40+
break
41+
42+
haveCount[word] += 1
43+
44+
if haveCount[word] > needCount[word]:
45+
break
46+
47+
right += wordLen
48+
49+
if right == left + windowLen:
50+
res.append(left)
51+
return res
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import collections
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def minWindow(self, s: str, t: str) -> str:
8+
"""
9+
Given two strings s and t of lengths m and n respectively, return the minimum
10+
window substring of s such that every character in t (including duplicates) is
11+
included in the window. If there is no such substring, return the empty
12+
string "".
13+
14+
The testcases will be generated such that the answer is unique.
15+
"""
16+
if len(s) < len(t):
17+
return ""
18+
19+
haveCount = collections.defaultdict(int)
20+
needCount = collections.defaultdict(int)
21+
for c in t:
22+
needCount[c] += 1
23+
24+
have, need = 0, len(needCount)
25+
res, resLen = [-1, -1], float("infinity")
26+
left, right = 0, 0
27+
while left < len(s) and right < len(s):
28+
c = s[right]
29+
haveCount[c] += 1
30+
if c in needCount and haveCount[c] == needCount[c]:
31+
have += 1
32+
while have == need:
33+
if (right - left + 1) < resLen:
34+
res = [left, right]
35+
resLen = right - left + 1
36+
c = s[left]
37+
haveCount[c] -= 1
38+
if c in needCount and haveCount[c] < needCount[c]:
39+
have -= 1
40+
left += 1
41+
right += 1
42+
left, right = res
43+
return s[left : right + 1] if resLen != float("infinity") else ""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._30_substring_with_concatenation_of_all_words import (
6+
Solution,
7+
)
8+
9+
10+
@pytest.mark.parametrize(
11+
argnames=["s", "words", "expected"],
12+
argvalues=[
13+
("barfoothefoobarman", ["foo", "bar"], [0, 9]),
14+
("wordgoodgoodgoodbestword", ["word", "good", "best", "word"], []),
15+
("barfoofoobarthefoobarman", ["bar", "foo", "the"], [6, 9, 12]),
16+
],
17+
)
18+
def test_func(s: str, words: List[str], expected: List[int]):
19+
"""Tests the solution of a LeetCode problem."""
20+
sub_strings = Solution().findSubstring(s, words)
21+
assert sub_strings == expected

Diff for: tests/test_76_minimum_window_substring.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._76_minimum_window_substring import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["s", "t", "expected"],
8+
argvalues=[
9+
("ADOBECODEBANC", "ABC", "BANC"),
10+
("a", "a", "a"),
11+
("a", "aa", ""),
12+
],
13+
)
14+
def test_func(s: str, t: str, expected: str):
15+
"""Tests the solution of a LeetCode problem."""
16+
minWindow = Solution().minWindow(s, t)
17+
assert minWindow == expected

0 commit comments

Comments
 (0)