Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef97199

Browse files
authored
Update 269-Alien-Dictionary.py
1 parent80fd573 commitef97199

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

‎python/269-Alien-Dictionary.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
11
classSolution:
22
defalienOrder(self,words:List[str])->str:
3-
adj= {char:set()forwordinwordsforcharinword}
3+
adj= {c:set()forwinwordsforcinw}
44

55
foriinrange(len(words)-1):
66
w1,w2=words[i],words[i+1]
77
minLen=min(len(w1),len(w2))
8+
89
iflen(w1)>len(w2)andw1[:minLen]==w2[:minLen]:
910
return""
11+
1012
forjinrange(minLen):
11-
ifw1[j]!=w2[j]:
12-
print(w1[j],w2[j])
13-
adj[w1[j]].add(w2[j])
13+
w1[j]!=w2[j]:
14+
adj[w1[j]].add(w2[j]]
1415
break
1516

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

19-
defdfs(char):
20-
ifcharinvisited:
21-
returnvisited[char]
20+
defdfs(c):
21+
ifcinvisit:
22+
returnvisit[c]
23+
24+
visit[c]=True
2225

23-
visited[char]=True
26+
forneiinadj[c]:
27+
ifdfs(nei):
28+
returnTrue
2429

25-
forneighCharinadj[char]:
26-
ifdfs(neighChar):
27-
returnTrue
30+
visit[c]=False
31+
res.append(c)
2832

29-
visited[char]=False
30-
res.append(char)
33+
forcinadj:
34+
ifdfs(c):
35+
return""
3136

32-
forcharinadj:
33-
ifdfs(char):
34-
return""
37+
return"".join(res)
3538

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp