Given a dictionary, find all of the longest words in the dictionary.
Given
{
“dog”,
“google”,
“facebook”,
“internationalization”,
“blabla”
}
the longest words are(is) [“internationalization”].
Given
{
“like”,
“love”,
“hate”,
“yes”
}
the longest words are [“like”, “love”, “hate”].
public class Solution {
/**
* @param dictionary: an array of strings
* @return: an arraylist of strings
*/
ArrayList<String> longestWords(String[] dictionary) {
int max = 0;
ArrayList<String> ans = new ArrayList ();
for (int i = 0;i < dictionary.length; i++ ) {
if (dictionary[i].length() >= max) {
max = dictionary [i].length();
}
}
for (int i = 0; i<dictionary.length ; i++ ) {
if (dictionary[i].length() == max){
ans.add(dictionary[i]);
}
}
return ans;
}
}