일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 잡생각
- 쓰릴오브파이트
- 30분
- 링피트
- 매일
- 영어원서읽기
- 사이드
- Problem Solving
- 운동
- 스탭퍼
- 월간
- leetcode
- 개발자
- English
- FIT XR
- 괜찮음
- Writing
- 파비최
- 프로젝트
- 영어공부
- 만화도
- realclass
- 리얼 클래스
- 10분
- 미드시청
- 화상영어
- Daily Challenge
- 읽기
- 3줄정리
- 뭐든
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.02.05 Today's Challenge 본문
https://leetcode.com/problems/find-all-anagrams-in-a-string/
Find All Anagrams in a String - LeetCode
Find All Anagrams in a String - Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order. An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase
leetcode.com
1. 해시맵 생성 : p는 key로, 빈도수는 values로
2. s에 대해 슬라이딩 윈도우 기법 적용
class Solution:
def findAnagrams(self, s: str, p: str) -> List[int]:
hm, res, pl, sl = defaultdict(int), [], len(p), len(s)
if pl > sl:
return []
for ch in p:
hm[ch] += 1
for i in range(pl - 1):
if s[i] in hm:
hm[s[i]] -= 1
for i in range(-1, sl-pl + 1):
if i > -1 and s[i] in hm:
hm[s[i]] += 1
if i+pl < sl and s[i+pl] in hm:
hm[s[i+pl]] -= 1
if all(v == 0 for v in hm.values()):
res.append(i+1)
return res
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
2023.02.07 Today's Challenge (0) | 2023.02.07 |
---|---|
2023.02.06 Today's Challenge (0) | 2023.02.06 |
2023.02.04 Today's Challenge (0) | 2023.02.04 |
2023.02.03 Today's Challenge (0) | 2023.02.03 |
2023.02.02 Today's Challenge (0) | 2023.02.02 |
Comments