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

Commit3f41215

Browse files
committed
269 Alien Dictionary python
1 parent53c3815 commit3f41215

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎python/269-Alien-Dictionary.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
classSolution:
2+
defalienOrder(self,words:List[str])->str:
3+
adj= {char:set()forwordinwordsforcharinword}
4+
5+
foriinrange(len(words)-1):
6+
w1,w2=words[i],words[i+1]
7+
minLen=min(len(w1),len(w2))
8+
iflen(w1)>len(w2)andw1[:minLen]==w2[:minLen]:
9+
return''
10+
forjinrange(minLen):
11+
ifw1[j]!=w2[j]:
12+
print(w1[j],w2[j])
13+
adj[w1[j]].add(w2[j])
14+
break
15+
16+
visited= {}# {char: bool} False visited, True current path
17+
res= []
18+
19+
defdfs(char):
20+
ifcharinvisited:
21+
returnvisited[char]
22+
23+
visited[char]=True
24+
25+
forneighCharinadj[char]:
26+
ifdfs(neighChar):
27+
returnTrue
28+
29+
visited[char]=False
30+
res.append(char)
31+
32+
forcharinadj:
33+
ifdfs(char):
34+
return''
35+
36+
res.reverse()
37+
return''.join(res)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp