일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 43. Multiply Strings
- kaggle
- DWG
- Decorator
- Python Code
- 파이썬
- data science
- Convert Sorted List to Binary Search Tree
- 시바견
- Protocol
- Substring with Concatenation of All Words
- LeetCode
- 밴픽
- iterator
- 프로그래머스
- 컴퓨터의 구조
- 운영체제
- 315. Count of Smaller Numbers After Self
- 109. Convert Sorted List to Binary Search Tree
- attribute
- t1
- Regular Expression
- Python Implementation
- 715. Range Module
- Generator
- shiba
- concurrency
- Python
- Class
- 30. Substring with Concatenation of All Words
Archives
- Today
- Total
Scribbling
LeetCode: 76. Minimum Window Substring 본문
First: O(N) solution.
However, is_satisfied function is not very efficient.
class Solution:
def minWindow(self, s, t):
ret = ''
from collections import Counter
c = Counter(t)
chars = c.keys()
j = 0
for i, char in enumerate(s):
if char in chars:
c[char] -= 1
if self.is_satisfied(c):
if not ret or (i - j + 1) <= len(ret):
ret = s[j:i+1]
while j <= i and self.is_satisfied(c):
if s[j] in chars:
c[s[j]] += 1
if not ret or (i - j + 1) <= len(ret):
ret = s[j:i+1]
j += 1
return ret
def is_satisfied(self, counter):
for k, v in counter.items():
if v > 0:
return False
return True
We can't check whether the substring satisfies the given condition with 'missing' variable.
class Solution:
def minWindow(self, s, t):
from collections import Counter
need, missing = Counter(t), len(t)
i, start, end = 0, 0, 0
for j, char in enumerate(s, 1):
if need[char] > 0:
missing -= 1
need[char] -= 1
if missing == 0:
while need[s[i]] < 0:
need[s[i]] += 1
i += 1
if end == 0 or j - i < end - start:
end, start = j, i
need[s[i]] += 1
missing += 1
i += 1
return s[start:end]
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 79. Word Search (0) | 2021.10.01 |
---|---|
LeetCode: 78. Subsets (0) | 2021.10.01 |
LeetCode: 77. Combinations (0) | 2021.09.28 |
LeetCode: 75. Sort Colors (0) | 2021.09.28 |
LeetCode: 74. Search a 2D Matrix (0) | 2021.09.28 |