(String arr[])
| 5 | |
| 6 | public class Question { |
| 7 | public static String printLongestWord(String arr[]) { |
| 8 | HashMap<String, Boolean> map = new HashMap<String, Boolean>(); |
| 9 | for (String str : arr) { |
| 10 | map.put(str, true); |
| 11 | } |
| 12 | Arrays.sort(arr, new LengthComparator()); // Sort by length |
| 13 | for (String s : arr) { |
| 14 | if (canBuildWord(s, true, map)) { |
| 15 | System.out.println(s); |
| 16 | return s; |
| 17 | } |
| 18 | } |
| 19 | return ""; |
| 20 | } |
| 21 | |
| 22 | public static boolean canBuildWord(String str, boolean isOriginalWord, HashMap<String, Boolean> map) { |
| 23 | if (map.containsKey(str) && !isOriginalWord) { |
no test coverage detected