Skip to content

Commit b883214

Browse files
committed
solve(w15): 269. Alien Dictionary
1 parent c18ec80 commit b883214

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# https://leetcode.com/problems/alien-dictionary/
2+
3+
from typing import List
4+
5+
class Solution:
6+
def alienOrder(self, words: List[str]) -> str:
7+
"""
8+
[Complexity]
9+
(V = words ๋‚ด unique ๋ฌธ์ž ๊ฐœ์ˆ˜, E = edge ๊ฐœ์ˆ˜, L = words๋‚ด ๋ชจ๋“  ๋ฌธ์ž ๊ฐœ์ˆ˜)
10+
- TC: O(V + L) (topo sort์— O(V + E) ์†Œ์š” -> E < L ์ด๋ฏ€๋กœ)
11+
- SC: O(V + E) (graph)
12+
13+
[Approach]
14+
topo sort๋ฅผ ์ด์šฉํ•˜์—ฌ, words์— ํฌํ•จ๋œ ์ „์ฒด ๋ฌธ์ž ๊ฐœ์ˆ˜์™€ topo sort ์ˆœ์œผ๋กœ ๋ฐฉ๋ฌธํ•œ ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
15+
1) directed graph๋ฅผ ๊ตฌ์„ฑํ•  ๋•Œ, words์—์„œ ๋‘ ์ธ์ ‘ํ•œ ๋‹จ์–ด๋ฅผ ๋น„๊ตํ•˜๋ฉฐ ์ฒซ ๋ฒˆ์งธ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ graph & indegree๋ฅผ ๊ธฐ๋กํ•œ๋‹ค. (sorted lexicographically)
16+
์ด๋•Œ, ์ˆœ์„œ resolving์ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ๋ฅผ ํŒ๋‹จํ•˜์—ฌ ๋น ๋ฅด๊ฒŒ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.
17+
(์ธ์ ‘ํ•œ ๋‘ ๋‹จ์–ด๋ฅผ ์ˆœ์„œ๋Œ€๋กœ w1, w2๋ผ๊ณ  ํ•  ๋•Œ, (1) w2๋ณด๋‹ค w1์ด ๋” ๊ธธ๋ฉด์„œ (2) w2๊ฐ€ w1์— ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ, w1์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„์— ์žˆ๋Š” ๋ฌธ์ž๋Š” ์ˆœ์„œ๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.)
18+
2) ์ด๋ ‡๊ฒŒ ๊ธฐ๋กํ•œ graph & indegree๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ topo sort๋กœ ๋ฐฉ๋ฌธํ•œ ๋ฌธ์ž ๊ฐœ์ˆ˜์™€ ์ „์ฒด ๋ฌธ์ž ๊ฐœ์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ cycle์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”์ง€ ํ™•์ธํ•˜๊ณ ,
19+
cycle์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜, cycle์ด ๋ฐœ์ƒํ–ˆ๋‹ค๋ฉด ๋นˆ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
20+
"""
21+
from collections import defaultdict, deque
22+
23+
# directed graph
24+
graph, indegree = {}, {}
25+
for word in words:
26+
for w in word:
27+
graph[w] = set()
28+
indegree[w] = 0
29+
30+
for i in range(len(words) - 1):
31+
w1, w2 = words[i], words[i + 1]
32+
# ๋‘ ์ธ์ ‘ํ•œ ๋‹จ์–ด๋ฅผ ๋น„๊ตํ•˜๋ฉด์„œ, ์ฒซ ๋ฒˆ์งธ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž๊ฐ€ ๋‚˜์˜ฌ ๋•Œ graph & indegree ๊ธฐ๋ก
33+
for j in range(min(len(w1), len(w2))):
34+
c1, c2 = w1[j], w2[j]
35+
# ์ฒซ ๋ฒˆ์งธ๋กœ ๋‹ค๋ฅธ ๋ฌธ์ž ๋ฐœ๊ฒฌ ์‹œ, ๊ธฐ๋ก ํ›„ ๋‹ค์Œ ๋‹จ์–ด๋กœ ๋„˜์–ด๊ฐ€๊ธฐ
36+
if c1 != c2:
37+
if c2 not in graph[c1]:
38+
graph[c1].add(c2)
39+
indegree[c2] += 1
40+
break
41+
# ์ˆœ์„œ๋ฅผ resolve ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ, ๋น ๋ฅด๊ฒŒ ๋ฆฌํ„ด *****
42+
# ex) words = ["abc", "ab"] (w1 = "abc", w2 = "ab")
43+
# -> (1) w2๋ณด๋‹ค w1์ด ๋” ๊ธธ๊ณ  (2) w2๊ฐ€ w1์— ํฌํ•จ๋œ๋‹ค๋ฉด (=ํ˜„์žฌ j๊ฐ€ w2์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค๋ฉด)
44+
# w1[j + 1] ์ดํ›„์˜ ๋ฌธ์ž์— ๋Œ€ํ•ด์„œ๋Š” ์ˆœ์„œ๋ฅผ resolve ํ•  ์ˆ˜ ์—†์Œ
45+
elif len(w1) > len(w2) and j == min(len(w1), len(w2)) - 1:
46+
return ""
47+
48+
# topo sort
49+
q = deque([w for w, ind in indegree.items() if ind == 0 and w in graph])
50+
res = []
51+
52+
while q:
53+
w = q.popleft()
54+
res.append(w)
55+
56+
for nw in graph[w]:
57+
indegree[nw] -= 1
58+
if indegree[nw] == 0:
59+
q.append(nw)
60+
61+
return "".join(res) if len(res) == len(graph) else ""

0 commit comments

Comments
ย (0)