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