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"]
.
Challenge
It's easy to solve it in two passes, can you do it in one pass?
class Solution { /** * @param dictionary: an array of strings * @return: an arraylist of strings */ ArrayList<String> longestWords(String[] dictionary) { // write your code here ArrayList<String> list = new ArrayList<String>(); if(dictionary == null || dictionary.length == 0){ return list; } int len = dictionary[0].length(); list.add(dictionary[0]); for(int i = 1 ; i < dictionary.length ; i++){ if(dictionary[i].length() > len){ list.clear(); list.add(dictionary[i]); len = dictionary[i].length(); } else if(dictionary[i].length() == len){ list.add(dictionary[i]); } } return list; } };