Skip to content

Commit

Permalink
2024-07-06 01:40:23
Browse files Browse the repository at this point in the history
Affected files:
.obsidian/workspace.json
src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md
src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md
  • Loading branch information
gyunseo committed Jul 5, 2024
1 parent 7f25ad3 commit 960c738
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 15 deletions.
30 changes: 15 additions & 15 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
"type": "split",
"children": [
{
"id": "2fc2c57290be6233",
"id": "0fb89b2a6940e666",
"type": "tabs",
"children": [
{
"id": "4de88d0967b6111b",
"id": "d150f0dbbe8f52e7",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/boj-10159-저울.md",
"file": "src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md",
"mode": "source",
"source": false
}
Expand All @@ -22,16 +22,16 @@
]
},
{
"id": "9dc6b127649d0dbd",
"id": "44994a6aa79bf7e6",
"type": "tabs",
"children": [
{
"id": "c75042212be2b48d",
"id": "e497ed46ee466b84",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "src/content/blog/leet-code-64-minimum-path-sum.md",
"file": "src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"mode": "source",
"source": false
}
Expand Down Expand Up @@ -103,7 +103,7 @@
"state": {
"type": "backlink",
"state": {
"file": "src/content/blog/leet-code-64-minimum-path-sum.md",
"file": "src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -120,7 +120,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "src/content/blog/leet-code-64-minimum-path-sum.md",
"file": "src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -143,7 +143,7 @@
"state": {
"type": "outline",
"state": {
"file": "src/content/blog/leet-code-64-minimum-path-sum.md"
"file": "src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md"
}
}
}
Expand All @@ -166,17 +166,18 @@
"table-editor-obsidian:Advanced Tables Toolbar": false
}
},
"active": "c75042212be2b48d",
"active": "e497ed46ee466b84",
"lastOpenFiles": [
"src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.md",
"src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md",
"src/content/blog/boj-10159-저울.md",
"src/content/blog/leet-code-64-minimum-path-sum.md",
"src/content/blog/leet-code-17-letter-combinations-of-a-phone-number.md",
"src/content/blog/leet-code-1438-longest-continuous-subarray-with-absolute-diff-less -than-or-equal-to-limit.md",
"src/content/blog/boj-2841-외계인의-기타-연주.md",
"src/content/blog/boj-10159-저울.md",
"src/content/blog/boj-1926-그림.md",
"src/content/blog/boj-11057-오르막-수.md",
"dist/posts/boj-백준-11507-오르막-수.png",
"src/content/blog/boj-11057-.md",
"src/content/blog/leet-code-64-minimum-path-sum.md",
"src/content/blog/boj-1940-주몽.md",
"src/content/blog/install-neovim-using-aqua-on-ubuntu.md",
"src/content/blog/how-to-connect-wifi-on-ubuntu-server.md",
Expand Down Expand Up @@ -212,7 +213,6 @@
"src/content/blog/boj-15663-N과-M-(9).md",
"src/content/blog/boj-11728-배열-합치기.md",
"src/content/blog/boj-10819-차이를-최대로.md",
"src/content/blog/boj-2230-수-고르기.md",
"src/content/blog/boj-1912-연속합.md"
"src/content/blog/boj-2230-수-고르기.md"
]
}
81 changes: 81 additions & 0 deletions src/content/blog/leet-code-438-find-all-anagrams-in-a-string.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
author: Gyunseo Lee
title: "LeetCode 438: Find All Anagrams in a String"
pubDatetime: 2024-07-06T01:34:00+09:00
modDatetime: 2024-07-06T01:34:00+09:00
featured: false
draft: false
tags:
- PS
- LeetCode
- Algorithms
- HashMap
- Sliding-Window
description: Sliding Window 문제!
ogImage: ""
---

## Table of contents

## 들어가며

걸린 시간: 45분 (자력으로 못 풀고, 솔루션을 찾아서 보고 참고해서 풀었습니다.)

처음 문제를 읽고, p의 길이만큼의 sliding window를 만들어서 풀면 order of N square에서 order of N으로 쉽게 바꿔서 풀 수 있겠다고 생각을 해서 들어 갔습니다.

## 접근

![](https://res.cloudinary.com/gyunseo-blog/image/upload/f_auto/v1720197539/image_jijiez.png)

그런데, window 안에 들어 있는 문자열이 p의 anagram인지 체크하는 로직을 order of N 보다 더 빠른 시간복잡도 안에 할 수 있는 방법을 떠올리지 못했습니다... 😥
그래서 솔루션을 봤는데, HashMap 두 개를 이용해서 해시맵을 비교해서 order of 1 안에 valid한 문자열인지(즉, p의 anagram인지) 체크를 하더군요.

## 구현

```python
from collections import defaultdict
class Solution:

def findAnagrams(self, s: str, p: str) -> List[int]:

def OOB(idx):
if idx < 0 or idx >= len_s:
return True
return False

len_s = len(s)
len_p = len(p)
p_hash_map = defaultdict(lambda: 0)
s_hash_map = defaultdict(lambda: 0)
for ch in p:
p_hash_map[ch] += 1
print(p_hash_map)

ans = []
start = end = 0

for start in range(len_s):

while not OOB(end) and end - start < len_p:
s_hash_map[s[end]] += 1
end += 1

# print(s_hash_map)
# O(26) = O(1)
if p_hash_map == s_hash_map:
ans.append(start)
# shift window
s_hash_map[s[start]] -= 1
if s_hash_map[s[start]] <= 0:
s_hash_map.pop(s[start])

return ans





```

슬라이딩 윈도우가 문자열 s를 훑으면서, window안에 있는 문자열이 valid한 문자열인지 확인한다라고 생각하면 편한 거 같습니다.
그래서 python의 대표적인 HashMap 자료 구조인 dictionary를 사용해서 구현을 했습니다.

0 comments on commit 960c738

Please sign in to comment.