MCPcopy
hub / github.com/careercup/ctci / createWordGroups

Method createWordGroups

java/Chapter 18/Question18_13/WordGroup.java:37–61  ·  view source on GitHub ↗
(String[] list)

Source from the content-addressed store, hash-verified

35 }
36
37 public static WordGroup[] createWordGroups(String[] list) {
38 WordGroup[] groupList;
39 int maxWordLength = 0;
40 // Find out the length of the longest word
41 for (int i = 0; i < list.length; i++) {
42 if (list[i].length() > maxWordLength) {
43 maxWordLength = list[i].length();
44 }
45 }
46
47 /* Group the words in the dictionary into lists of words of
48 * same length.groupList[i] will contain a list of words, each
49 * of length (i+1). */
50 groupList = new WordGroup[maxWordLength];
51 for (int i = 0; i < list.length; i++) {
52 /* We do wordLength - 1 instead of just wordLength since this is used as
53 * an index and no words are of length 0 */
54 int wordLength = list[i].length() - 1;
55 if (groupList[wordLength] == null) {
56 groupList[wordLength] = new WordGroup();
57 }
58 groupList[wordLength].addWord(list[i]);
59 }
60 return groupList;
61 }
62}

Callers 1

QuestionMethod · 0.95

Calls 2

lengthMethod · 0.45
addWordMethod · 0.45

Tested by

no test coverage detected