Skip to content

Commit e5260e7

Browse files
committed
alien-dictionary solution
1 parent 9250b9e commit e5260e7

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

โ€Žalien-dictionary/yyyyyyyyyKim.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
LintCode 892. Alien Dictionary
3+
https://www.lintcode.com/problem/892/
4+
https://leetcode.com/problems/alien-dictionary/
5+
6+
summary:
7+
์™ธ๊ณ„์ธ ๋‹จ์–ด ๋ชฉ๋ก์ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜์–ด ์žˆ์„ ๋•Œ,
8+
๋‹จ์–ด ๊ฐ„์˜ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋ฌธ์ž์˜ ์ˆœ์„œ๋ฅผ ์œ ์ถ”ํ•ด ์ „์ฒด ์™ธ๊ณ„์ธ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ
9+
"""
10+
from typing import (
11+
List,
12+
)
13+
14+
class Solution:
15+
"""
16+
@param words: a list of words
17+
@return: a string which is correct order
18+
"""
19+
def alien_order(self, words: List[str]) -> str:
20+
21+
# ๋ฌด์กฐ๊ฑด๋‹ค์‹œํ’€์–ด๋ณผ๊ฒƒ. ์–ด๋ ค์›€์–ด๋ ค์›€.
22+
# DFS๊ธฐ๋ฐ˜ ํ›„์œ„์ˆœํšŒ(post-order) ์œ„์ƒ ์ •๋ ฌ
23+
# ์‹œ๊ฐ„๋ณต์žก๋„ O(N*L+V+E), ๊ณต๊ฐ„๋ณต์žก๋„ O(V+E)
24+
# V = ๋“ฑ์žฅํ•œ ๋ฌธ์ž์˜ ์ˆ˜(์ตœ๋Œ€26๊ฐœ), E = ๊ฐ„์„ ์ˆ˜(๋ฌธ์ž๊ฐ„์ˆœ์„œ), N = ๋‹จ์–ด์ˆ˜, L = ๊ฐ ๋‹จ์–ด์˜ ํ‰๊ท  ๊ธธ์ด
25+
26+
# ๊ทธ๋ž˜ํ”„ ๋งŒ๋“ค๊ธฐ(๋ฌธ์ž๋ณ„ ์—ฐ๊ฒฐ ์ •๋ณด)
27+
graph = {}
28+
29+
# visited
30+
# 0: ๋ฐฉ๋ฌธ ์•ˆํ•จ
31+
# 1: ๋ฐฉ๋ฌธ ์ค‘(ํ˜„์žฌ ํƒ์ƒ‰ ์ค‘)
32+
# 2: ๋ฐฉ๋ฌธ ์™„๋ฃŒ(์‚ฌ์ดํด ์—†์Œ ํ™•์ธ ์™„๋ฃŒ)
33+
visited = {}
34+
answer = []
35+
36+
# ๋…ธ๋“œ ์ดˆ๊ธฐํ™”(๋ชจ๋“  ๋ฌธ์ž ๊ทธ๋ž˜ํ”„์— ๋„ฃ๊ธฐ)
37+
for word in words:
38+
for i in word:
39+
if i not in graph: # ์—†์œผ๋ฉด ๋„ฃ๊ธฐ(์ค‘๋ณต๋ฐฉ์ง€)
40+
graph[i] = []
41+
42+
# ๋ฌธ์ž ๊ฐ„์„  ์„ค์ •
43+
for i in range(len(words)-1):
44+
w1 = words[i]
45+
w2 = words[i+1]
46+
min_len = min(len(w1), len(w2))
47+
48+
# ์•ž๋‹จ์–ด๊ฐ€ ๋’ท๋‹จ์–ด์˜ ์ ‘๋‘์‚ฌ์ธ๋ฐ ๋” ๊ธธ๋ฉด ์ž˜๋ชป๋œ ์ž…๋ ฅ์ด๋ฏ€๋กœ '' ๋ฐ˜ํ™˜
49+
if w1[:min_len] == w2[:min_len] and len(w1) > len(w2):
50+
return ''
51+
52+
for j in range(min_len):
53+
c1 = w1[j]
54+
c2 = w2[j]
55+
# c1๊ณผ c2๊ฐ€ ๋‹ค๋ฅธ ๋ฌธ์ž์ด๊ณ  c1์— c2๊ฐ€ ์—†์œผ๋ฉด c1์˜ ๊ทธ๋ž˜ํ”„์— c2 ์ถ”๊ฐ€
56+
if c1 != c2:
57+
if c2 not in graph[c1]:
58+
graph[c1].append(c2)
59+
break
60+
61+
# ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ๊ฒฝ์šฐ, ์‚ฌ์ „์ˆœ ํƒ์ƒ‰์„ ์œ„ํ•ด ์ž์‹ ๋…ธ๋“œ ์ •๋ ฌ
62+
for i in graph:
63+
graph[i].sort()
64+
65+
# DFS ํ•จ์ˆ˜
66+
def dfs(node):
67+
# visited.get(node, 0) : ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’ 0 ๋ฐ˜ํ™˜
68+
if visited.get(node, 0) == 1:
69+
return False # ์‚ฌ์ดํด ๋ฐœ์ƒ
70+
if visited.get(node, 0) == 2:
71+
return True # ๋ฐฉ๋ฌธ ์™„๋ฃŒ
72+
73+
# ๋ฐฉ๋ฌธ ์ค‘ ํ‘œ์‹œ
74+
visited[node] = 1
75+
76+
# ์ธ์ ‘ ๋…ธ๋“œ(๋‹ค์Œ๋ฌธ์ž) ํƒ์ƒ‰
77+
for i in graph[node]:
78+
if not dfs(i):
79+
return False
80+
81+
# ํƒ์ƒ‰ ์™„๋ฃŒ(๋ฐฉ๋ฌธ ์™„๋ฃŒ)
82+
visited[node] = 2
83+
answer.append(node)
84+
85+
return True
86+
87+
# ๋ชจ๋“  ๋…ธ๋“œ dfs ํƒ์ƒ‰
88+
for i in graph:
89+
if visited.get(i, 0) == 0:
90+
if not dfs(i):
91+
return '' # ์‚ฌ์ดํด์ด๋ฉด ์ž˜๋ชป๋œ ์ž…๋ ฅ
92+
93+
94+
# post-order๋‹ˆ๊นŒ ์—ญ์ˆœ์œผ๋กœ ๋ฐ˜ํ™˜
95+
return ''.join(reversed(answer))

0 commit comments

Comments
ย (0)