| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- attribute
- 109. Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Decorator
- shiba
- 밴픽
- Substring with Concatenation of All Words
- iterator
- Python
- DWG
- 컴퓨터의 구조
- data science
- 30. Substring with Concatenation of All Words
- Python Code
- t1
- Python Implementation
- 파이썬
- 프로그래머스
- Protocol
- 315. Count of Smaller Numbers After Self
- 715. Range Module
- concurrency
- 43. Multiply Strings
- kaggle
- Class
- 시바견
- 운영체제
- Generator
- Regular Expression
- LeetCode
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 |