-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path720.py
35 lines (35 loc) · 1.31 KB
/
720.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
__________________________________________________________________________________________________
sample 68 ms submission
class Solution:
def longestWord(self, words: List[str]) -> str:
ans = ""
word_set = set(words)
for word in words:
if len(word) > len(ans) or len(word) == len(ans) and word < ans:
can_built = True
for k in range(1, len(word)):
if word[:k] not in word_set:
can_built = False
break
if can_built:
ans = word
return ans
__________________________________________________________________________________________________
sample 13324 kb submission
class Solution:
def longestWord(self, words: List[str]) -> str:
out=[]
for w in words:
x=[w[:i] for i in range(1,len(w))]
if x==[]:
continue
else:
if all(i in words for i in x):
out.append(w)
if out==[]:
return sorted(words)[0]
maxlen=max([len(x) for x in out])
out2=[x for x in out if len(x)==maxlen]
out2=sorted(out2)
return out2[0]
__________________________________________________________________________________________________