forked from algorhythms/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest Words.py
More file actions
45 lines (36 loc) · 782 Bytes
/
Longest Words.py
File metadata and controls
45 lines (36 loc) · 782 Bytes
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
36
37
38
39
40
41
42
43
44
45
"""
Given a dictionary, find all of the longest words in the dictionary.
Have you met this question in a real interview? Yes
Example
Given
{
"dog",
"google",
"facebook",
"internationalization",
"blabla"
}
the longest words are(is) ["internationalization"].
Given
{
"like",
"love",
"hate",
"yes"
}
the longest words are ["like", "love", "hate"].
"""
__author__ = 'Daniel'
class Solution:
def longestWords(self, dictionary):
"""
:param dictionary: a list of strings
:return: a list of strings
"""
ret = []
for word in dictionary:
if not ret or len(word) > len(ret[0]):
ret = [word]
elif len(word) == len(ret[0]):
ret.append(word)
return ret