MCPcopy Index your code
hub / github.com/careercup/ctci / printLongestWord

Method printLongestWord

java/Chapter 18/Question18_7/Question.java:7–20  ·  view source on GitHub ↗
(String arr[])

Source from the content-addressed store, hash-verified

5
6public 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) {

Callers 1

mainMethod · 0.95

Calls 3

canBuildWordMethod · 0.95
putMethod · 0.80
sortMethod · 0.45

Tested by

no test coverage detected