| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- Decorator
- 715. Range Module
- Python Implementation
- attribute
- 파이썬
- 43. Multiply Strings
- Generator
- 밴픽
- Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- Class
- shiba
- t1
- 시바견
- Protocol
- data science
- LeetCode
- concurrency
- 30. Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- iterator
- kaggle
- DWG
- 315. Count of Smaller Numbers After Self
- Python
- Regular Expression
- 컴퓨터의 구조
- 운영체제
- Python Code
- 프로그래머스
Archives
- Today
- Total
Scribbling
LeetCode: 30. Substring with Concatenation of All Words 본문
Computer Science/Coding Test
LeetCode: 30. Substring with Concatenation of All Words
focalpoint 2022. 1. 17. 14:40A solution using sliding window.
class Solution:
def findSubstring(self, s: str, words: List[str]) -> List[int]:
from collections import Counter
ret = []
wordSet = set(words)
n, m = len(words), len(words[0])
for i in range(m):
wordCounter = Counter(words)
k = j = i
while j + m <= len(s):
if j - k >= n * m:
if s[k:k+m] in wordSet:
wordCounter[s[k:k+m]] += 1
k += m
word = s[j:j+m]
if word in wordSet:
wordCounter[word] -= 1
j += m
if self.is_Finish(wordSet, wordCounter):
ret.append(j - n * m)
return ret
def is_Finish(self, wordSet, wordCounter):
for word in wordSet:
if wordCounter[word] != 0:
return False
return True

'Computer Science > Coding Test' 카테고리의 다른 글
| LeetCode: 302. Smallest Rectangle Enclosing Black Pixels (0) | 2022.01.25 |
|---|---|
| LeetCode: 65. Valid Number (0) | 2022.01.20 |
| LeetCode: 27. Remove Element (0) | 2022.01.17 |
| LeetCode: 454. 4Sum II (0) | 2022.01.16 |
| LeetCode: 395. Longest Substring with At Least K Repeating Characters (0) | 2022.01.16 |