|
| 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