Longest Words
Source
Given a dictionary, find all of the longest words in the dictionary.
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"].
Challenge
It's easy to solve it in two passes, can you do it in one pass?
Python
class Solution:
def longestWords(self, dictionary):
result=[]
if dictionary is None or len(dictionary)==0:
return result
for s in dictionary:
if len(result)==0 or len(s)>len(result[0]):
result=[]
result.append(s)
elif len(s)==len(result[0]):
result.append(s)
return result