Skip to content

Commit 08a7d6c

Browse files
committed
[7th batch] week 4 - word search
1 parent cf57e14 commit 08a7d6c

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

โ€Žword-search/liza0525.pyโ€Ž

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,73 @@ def dfs(i, j, str_index):
6565

6666
# ๋ชจ๋“  ์‹œ๋„๋ฅผ ํ•ด๋„ ๋ชป ์ฐพ์œผ๋ฉด False
6767
return False
68+
69+
70+
# 7๊ธฐ ํ’€์ด
71+
# ์‹œ๊ฐ„ ๋ณต์žก๋„: O((n * m) * 4 ^ d)
72+
# - depth์˜ ๊ธธ์ด(d)๋งŒํผ ๋„ค ๋ฐฉํ–ฅ์„ ํƒ์ƒ‰ํ•˜๋ฉฐ, ์ถœ๋ฐœ์ ์˜ ๊ฐœ์ˆ˜ n * m ๋ชจ๋‘ ํƒ์ƒ‰ํ•  ๋•Œ ์ตœ์•…์˜ ์‹œ๊ฐ„
73+
# ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n * m)
74+
# - visited ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ–‰ * ์—ด์˜ ๊ฐœ์ˆ˜๋งŒํผ์˜ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•จ
75+
class Solution:
76+
def exist(self, board: List[List[str]], word: str) -> bool:
77+
# ๋ณด๋“œ์˜ ํ–‰, ์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฏธ๋ฆฌ ์ €์žฅ
78+
num_rows = len(board)
79+
num_cols = len(board[0])
80+
81+
# dfs ํƒ์ƒ‰ ์‹œ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ฒฝ์šฐ๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•œ visited๋ฅผ ์ƒ์„ฑ
82+
visited = [[0 for _ in range(num_cols)] for _ in range(num_rows)]
83+
84+
# dfs ํƒ์ƒ‰ ์‹œ ํ•œ ์นธ์„ ๊ธฐ์ค€์œผ๋กœ ํ•˜์—ฌ 4๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ ๊ฐ€๋Šฅํ•˜๊ฒŒ directions์„ ๋งŒ๋“ ๋‹ค.
85+
# ์ˆœ์„œ๋Œ€๋กœ ์•„๋ž˜๋กœ ์ง„์ถœ, ์œ„๋กœ ์ง„์ถœ, ์™ผ์ชฝ์œผ๋กœ ์ง„์ถœ, ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ง„์ถœ
86+
directions = [(0, 1), (0, -1), (-1, 0), (1, 0)]
87+
88+
def check_exist_word(i, j, depth):
89+
if depth == len(word) - 1:
90+
# depth๊ฐ€ word์˜ ๊ธธ์ด์— ๊ฐ€๊นŒ์›Œ์ง€๋ฉด(๋ฐฐ์—ด์ด๋ผ -1 ํ•ด์คŒ)
91+
# ํƒ์ƒ‰์„ ๋งˆ์ณค์œผ๋ฏ€๋กœ True๋ฅผ ๋ฐ˜ํ™˜
92+
return True
93+
94+
# ํ˜„์žฌ์˜ ์นธ์€ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๋Š” ์˜๋ฏธ๋กœ 1๋กœ ๋ณ€๊ฒฝ
95+
visited[i][j] = 1
96+
for dir_i, dir_j in directions:
97+
# ๋„ค ๋ฐฉํ–ฅ์„ ์ˆœ์ฐจ๋กœ ํƒ์ƒ‰ํ•œ๋‹ค.
98+
# ๋‹ค์Œ ์นธ์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” next_i์™€ next_j๋ฅผ ๋จผ์ € ๊ณ„์‚ฐ
99+
next_i, next_j = i + dir_i, j + dir_j
100+
if not (
101+
0 <= next_i < num_rows # ๋‹ค์Œ ์นธ์˜ ํ–‰ ๋ฒˆํ˜ธ๊ฐ€ ํ–‰์˜ ๊ธธ์ด ๋‚ด์— ์žˆ๊ณ 
102+
and 0 <= next_j < num_cols # ๋‹ค์Œ ์นธ์˜ ์—ด ๋ฒˆํ˜ธ๊ฐ€ ์—ด์˜ ๊ธธ์ด ๋‚ด์— ์žˆ๊ณ 
103+
and not visited[next_i][next_j] # ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉฐ
104+
and board[next_i][next_j] == word[depth + 1] # ๋‹ค์Œ ๊ธ€์ž๊ฐ€ word์˜ ๋‹ค์Œ ๊ธ€์ž์— ํ•ด๋‹นํ•ด์•ผ ํ•จ
105+
):
106+
# ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ํƒ์ƒ‰ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋‹ค์Œ ๋ฐฉํ–ฅ์„ ์ฐพ๋Š”๋‹ค
107+
continue
108+
109+
# ์œ„์˜ ์กฐ๊ฑด์„ ๋ชจ๋‘ ๋งŒ์กฑํ•˜๋ฉด ํƒ์ƒ‰ํ•ด๋„ ๋œ๋‹ค.
110+
exist_word = check_exist_word(
111+
next_i,
112+
next_j,
113+
depth + 1 # word์˜ ๋‹ค์Œ index๋ฅผ ์ฐพ์•„์•ผ ํ•จ
114+
)
115+
if exist_word:
116+
# ๋‹จ์–ด๋ฅผ ์ฐพ์•˜๋‹ค๊ณ  ํ•˜๋ฉด early return
117+
return True
118+
119+
# ๋ฐฑํŠธ๋ž˜ํ‚น์„ ์œ„ํ•ด ํ˜„์žฌ ์นธ์˜ visited ๊ฐ’์„ ์›๋ณตํ•œ๋‹ค.
120+
visited[i][j] = 0
121+
122+
# ๋‹จ์–ด๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์„ ๋• False ๋ฐ˜ํ™˜
123+
return False
124+
125+
for i in range(num_rows):
126+
for j in range(num_cols):
127+
if board[i][j] != word[0]:
128+
# ์ถœ๋ฐœ ๊ธ€์ž๊ฐ€ word์˜ ์ฒซ ๊ธ€์ž๋ž‘ ๊ฐ™์ง€ ์•Š์œผ๋ฉด ํƒ์ƒ‰ํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ๋„˜๊ธด๋‹ค.
129+
continue
130+
131+
exist_word = check_exist_word(i, j, 0)
132+
if exist_word:
133+
# ๋‹จ์–ด๋ฅผ ์ฐพ์•˜์œผ๋ฉด ํƒ์ƒ‰์„ ๋”์ด์ƒ ํ•˜์ง€ ์•Š์•„๋„ ๋˜๋ฏ€๋กœ early return
134+
return True
135+
136+
# ๋ณด๋“œ์˜ ๋ชจ๋“  ํƒ์ƒ‰์„ ํ•ด๋„ ์ฐพ์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ์—๋Š” False ๋ฐ˜ํ™˜
137+
return False

0 commit comments

Comments
ย (0)