Skip to content

Commit 9c035ae

Browse files
committed
Revert "Update 269-Alien-Dictionary.py"
This reverts commit ef97199.
1 parent ef97199 commit 9c035ae

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

python/269-Alien-Dictionary.py

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
class Solution:
22
def alienOrder(self, words: List[str]) -> str:
3-
adj = { c:set() for w in words for c in w }
3+
adj = {char: set() for word in words for char in word}
44

55
for i in range(len(words) - 1):
66
w1, w2 = words[i], words[i + 1]
77
minLen = min(len(w1), len(w2))
8-
98
if len(w1) > len(w2) and w1[:minLen] == w2[:minLen]:
109
return ""
11-
1210
for j in range(minLen):
13-
w1[j] != w2[j]:
14-
adj[w1[j]].add(w2[j]]
11+
if w1[j] != w2[j]:
12+
print(w1[j], w2[j])
13+
adj[w1[j]].add(w2[j])
1514
break
1615

17-
visit = {} # False=visited, True=current path
18-
res = []
16+
visited = {} # {char: bool} False visited, True current path
17+
res = []
1918

20-
def dfs(c):
21-
if c in visit:
22-
return visit[c]
23-
24-
visit[c] = True
19+
def dfs(char):
20+
if char in visited:
21+
return visited[char]
2522

26-
for nei in adj[c]:
27-
if dfs(nei):
28-
return True
23+
visited[char] = True
2924

30-
visit[c] = False
31-
res.append(c)
25+
for neighChar in adj[char]:
26+
if dfs(neighChar):
27+
return True
3228

33-
for c in adj:
34-
if dfs(c):
35-
return ""
29+
visited[char] = False
30+
res.append(char)
3631

37-
return "".join(res)
32+
for char in adj:
33+
if dfs(char):
34+
return ""
3835

39-
36+
res.reverse()
37+
return "".join(res)

0 commit comments

Comments
 (0)