File tree Expand file tree Collapse file tree 2 files changed +80
-12
lines changed Expand file tree Collapse file tree 2 files changed +80
-12
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Constraints:
3
+ - m == board.length
4
+ - n == board[i].length
5
+ - 1 <= m, n <= 12
6
+ - board[i][j] is a lowercase English letter.
7
+ - 1 <= words.length <= 3 * 10^4
8
+ - 1 <= words[i].length <= 10
9
+ - words[i] consists of lowercase English letters.
10
+ - All the strings of words are unique.
11
+
12
+ Time Complexity: O(W * N * 4^L)
13
+ - W는 words의 개수
14
+ - N은 board의 모든 cell (m * n)
15
+ - L은 word의 길이
16
+ - 각 단어마다 Word Search 1을 반복
17
+
18
+ Space Complexity: O(L)
19
+ - L은 word의 길이로, 재귀 호출 스택의 깊이
20
+
21
+ Word search 1과의 차이점:
22
+ - 단어 여러개를 동시에 찾아야 함
23
+ - 찾은 모든 단어들을 리스트로 반환해야 함
24
+
25
+ 풀이방법:
26
+ - Word search 1과 동일한 방법 + set(words)로 중복 제거
27
+
28
+ 노트:
29
+ - 시간초과로 통과 안 됨
30
+ - Trie 자료구조로 다시 풀어보기
31
+ """
32
+ class Solution :
33
+ def findWords (self , board : List [List [str ]], words : List [str ]) -> List [str ]:
34
+ words = list (set (words ))
35
+ result = []
36
+
37
+ def exist (word ):
38
+ rows , cols = len (board ), len (board [0 ])
39
+
40
+ def dfs (i , j , k ):
41
+ if k == len (word ):
42
+ return True
43
+
44
+ if (i < 0 or i >= rows or
45
+ j < 0 or j >= cols or
46
+ board [i ][j ] != word [k ]):
47
+ return False
48
+
49
+ temp = board [i ][j ]
50
+ board [i ][j ] = '#'
51
+
52
+ result = (dfs (i + 1 , j , k + 1 ) or
53
+ dfs (i - 1 , j , k + 1 ) or
54
+ dfs (i , j + 1 , k + 1 ) or
55
+ dfs (i , j - 1 , k + 1 ))
56
+
57
+ board [i ][j ] = temp
58
+ return result
59
+
60
+ for i in range (rows ):
61
+ for j in range (cols ):
62
+ if board [i ][j ] == word [0 ]:
63
+ if dfs (i , j , 0 ):
64
+ return True
65
+ return False
66
+
67
+ for word in words :
68
+ if exist (word ):
69
+ result .append (word )
70
+ return result
Original file line number Diff line number Diff line change 6
6
4. word length is between 1 and 15 inclusive
7
7
5. board and word contain only lowercase and uppercase English letters
8
8
9
- Time Complexity: O(N * 3 ^L)
9
+ Time Complexity: O(N * 4 ^L)
10
10
- N은 board의 모든 cell (m * n)
11
11
- L은 word의 길이
12
- - 각 cell에서 시작하여 word의 각 글자마다 세방향으로 탐색 (이미 방문한 방향 제외)
12
+ - 각 cell에서 시작하여 word의 각 글자마다 네방향으로 탐색 (이미 방문한 방향 제외)
13
13
14
14
Space Complexity: O(L)
15
15
- L은 word의 길이로, 재귀 호출 스택의 깊이
16
-
17
- To Do:
18
- - DFS와 백트래킹 개념 복습하기
19
- - 다른 스터디원분들의 답안 참조하여 다른 풀이방법 복습하기
20
16
"""
21
17
22
18
class Solution :
23
19
def exist (self , board : List [List [str ]], word : str ) -> bool :
24
20
rows , cols = len (board ), len (board [0 ])
25
21
26
22
def dfs (i , j , k ):
23
+
24
+ # 단어를 모두 찾았으면 True 반환
27
25
if k == len (word ):
28
26
return True
29
27
30
- if (i < 0 or i >= rows or
28
+ if (i < 0 or i >= rows or # 경계체크
31
29
j < 0 or j >= cols or
32
- board [i ][j ] != word [k ]):
30
+ board [i ][j ] != word [k ]): # 현재 문자가 찾는 문자와 다름
33
31
return False
34
32
35
- temp = board [i ][j ]
36
- board [i ][j ] = '#'
33
+ temp = board [i ][j ] # 현재 문자를 임시저장
34
+ board [i ][j ] = '#' # 방문 표시
37
35
38
- result = (dfs (i + 1 , j , k + 1 ) or
36
+ result = (dfs (i + 1 , j , k + 1 ) or # 상하좌우 네 방향 탐색
39
37
dfs (i - 1 , j , k + 1 ) or
40
38
dfs (i , j + 1 , k + 1 ) or
41
39
dfs (i , j - 1 , k + 1 ))
42
40
43
- board [i ][j ] = temp
41
+ board [i ][j ] = temp # 백트래킹 (원래 문자로 복원)
44
42
return result
45
43
46
44
for i in range (rows ):
You can’t perform that action at this time.
0 commit comments