|
| 1 | +packagepackage1; |
| 2 | + |
| 3 | +importjava.io.BufferedReader; |
| 4 | +importjava.io.BufferedWriter; |
| 5 | +importjava.io.File; |
| 6 | +importjava.io.FileReader; |
| 7 | +importjava.io.FileWriter; |
| 8 | +importjava.io.IOException; |
| 9 | +importjava.util.ArrayList; |
| 10 | +importjava.util.Collections; |
| 11 | +importjava.util.Comparator; |
| 12 | +importjava.util.HashSet; |
| 13 | +importjava.util.List; |
| 14 | +importjava.util.Set; |
| 15 | + |
| 16 | +publicclassConcatenatedWordsII { |
| 17 | + |
| 18 | +publicstaticvoidmain(String...args){ |
| 19 | +try { |
| 20 | +StringFILE_PATH ="/Users/SteveSun/Google Drive/Developer/wordsforproblem.txt"; |
| 21 | +// String FILE_PATH = "/Users/SteveSun/Google Drive/Developer/first30.txt"; |
| 22 | +// String FILE_PATH = "/Users/SteveSun/Google Drive/Developer/first50.txt"; |
| 23 | +// String FILE_PATH = "/Users/SteveSun/Google Drive/Developer/first100.txt"; |
| 24 | +// String FILE_PATH = "/Users/SteveSun/Google Drive/Developer/test.txt"; |
| 25 | + |
| 26 | +StringOUTPUT_PATH ="/Users/SteveSun/Google Drive/Developer/output_wordsforproblem.txt"; |
| 27 | +// String OUTPUT_PATH = "/Users/SteveSun/Google Drive/Developer/output_first30.txt"; |
| 28 | +// String OUTPUT_PATH = "/Users/SteveSun/Google Drive/Developer/output_first50.txt"; |
| 29 | +// String OUTPUT_PATH = "/Users/SteveSun/Google Drive/Developer/output_first1001.txt"; |
| 30 | +// String OUTPUT_PATH = "/Users/SteveSun/Google Drive/Developer/output_testagain.txt"; |
| 31 | + |
| 32 | +ConcatenatedWordsIItest =newConcatenatedWordsII(); |
| 33 | +ResultTyperesult =test.readFromFile(FILE_PATH); |
| 34 | +Set<String>words =result.wordDict; |
| 35 | +IntegermaxWordLen =result.maxWordLen; |
| 36 | +TrieNoderoot =test.buildTrie(words); |
| 37 | +longstartTime =System.currentTimeMillis(); |
| 38 | +List<String>validConcatenatedWords =newArrayList(); |
| 39 | +for (Stringword :words) { |
| 40 | +if (word ==null ||word.length() ==0)continue; |
| 41 | +test.remove(word,root);/** every word is comprised of every word itself, thus this word itself needs to be removed first for checking it*/ |
| 42 | +intn =word.length(); |
| 43 | +boolean[]dp =newboolean[n +1]; |
| 44 | +dp[0] =true; |
| 45 | + |
| 46 | +for (inti =1;i <=n;i++) { |
| 47 | +for (intj =1;j <=i &&j <=maxWordLen;j++) { |
| 48 | +if (!dp[i -j]) |
| 49 | +continue; |
| 50 | + |
| 51 | +StringsubWord =word.substring(i -j,i); |
| 52 | +if (test.contains(subWord,root)) { |
| 53 | +dp[i] =true; |
| 54 | +break; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +if(dp[n])validConcatenatedWords.add(word); |
| 60 | +test.undoRemove(word,root); |
| 61 | + } |
| 62 | +longfinishTime =System.currentTimeMillis(); |
| 63 | +longusedTime =finishTime -startTime; |
| 64 | + |
| 65 | +//Note: we're only interested in the first two longest words, so we can iterate through validConcatenatedWords once and find those two |
| 66 | +StringfirstLongest =""; |
| 67 | +StringsecondLongest =""; |
| 68 | +for(Stringword :validConcatenatedWords){ |
| 69 | +if(word.length() >secondLongest.length()){ |
| 70 | +if(word.length() >firstLongest.length()) { |
| 71 | +Stringtemp =secondLongest; |
| 72 | +secondLongest =firstLongest; |
| 73 | +firstLongest =word; |
| 74 | + }else { |
| 75 | +secondLongest =word; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | +//print out the result we're interested in: longest concatenated word, second concatenated word, and the total number of all concatenated words |
| 80 | +System.out.println("The longest concatenated word is: " +firstLongest +",\nthe 2nd longest concatenated word is: "+ |
| 81 | +secondLongest +"\nand there are a total of " +validConcatenatedWords.size() +" valid concatenated words in this given file."); |
| 82 | + |
| 83 | +//For a more verbose solution, I'm printing all words in descending order based on their lengths. |
| 84 | +sortAndWriteToFile(OUTPUT_PATH,validConcatenatedWords,usedTime); |
| 85 | + |
| 86 | + }catch (Exceptione) { |
| 87 | +e.printStackTrace(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | +privatestaticvoidsortAndWriteToFile(StringOUTPUT_PATH,List<String>validConcatenatedWords, |
| 92 | +longusedTime)throwsIOException { |
| 93 | +Collections.sort(validConcatenatedWords,newComparator<String>() { |
| 94 | +@Override |
| 95 | +publicintcompare(Stringo1,Stringo2) { |
| 96 | +if (o1.length() >o2.length()) { |
| 97 | +return -1; |
| 98 | + }elseif (o1.length() <o2.length()) { |
| 99 | +return1; |
| 100 | + }else { |
| 101 | +return0; |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | +Filefile =newFile(OUTPUT_PATH); |
| 107 | + |
| 108 | +if (!file.exists()) |
| 109 | +file.createNewFile(); |
| 110 | + |
| 111 | +FileWriterfw =newFileWriter(file.getAbsoluteFile()); |
| 112 | +BufferedWriterbw =newBufferedWriter(fw); |
| 113 | +bw.write("There's a total of " +validConcatenatedWords.size() +" valid concatenated words found in this file.\n"); |
| 114 | +bw.write("It took " +usedTime +" ms to finish.\n"); |
| 115 | +for (Stringword :validConcatenatedWords) { |
| 116 | +bw.write(word +"\n"); |
| 117 | + } |
| 118 | + |
| 119 | +bw.close(); |
| 120 | + } |
| 121 | + |
| 122 | +publicTrieNodebuildTrie(Set<String>words) { |
| 123 | +TrieNoderoot =newTrieNode(); |
| 124 | +for(Stringword :words){ |
| 125 | +char[]chars =word.toCharArray(); |
| 126 | +TrieNodenode =root; |
| 127 | +for(inti =0;i <chars.length;i++){ |
| 128 | +charc =chars[i]; |
| 129 | +if(node.children[c -'a'] ==null){ |
| 130 | +node.children[c -'a'] =newTrieNode(); |
| 131 | + } |
| 132 | +node =node.children[c -'a']; |
| 133 | + } |
| 134 | +node.isWord =true; |
| 135 | + } |
| 136 | +returnroot; |
| 137 | + } |
| 138 | + |
| 139 | +// Returns true if the word is in the trie. |
| 140 | +publicbooleancontains(Stringword,TrieNoderoot) { |
| 141 | +TrieNodenode =root; |
| 142 | +for(inti =0;i <word.length();i++){ |
| 143 | +if(node.children[word.charAt(i) -'a'] ==null)returnfalse; |
| 144 | +node =node.children[word.charAt(i) -'a']; |
| 145 | + } |
| 146 | +returnnode.isWord; |
| 147 | + } |
| 148 | + |
| 149 | +// mark that word on |
| 150 | +publicvoidundoRemove(Stringword,TrieNoderoot) { |
| 151 | +TrieNodenode =root; |
| 152 | +for(inti =0;i <word.length();i++){ |
| 153 | +node =node.children[word.charAt(i) -'a']; |
| 154 | + } |
| 155 | +node.isWord =true; |
| 156 | + } |
| 157 | + |
| 158 | +// mark that word off, we are not really deleting that word |
| 159 | +publicvoidremove(Stringword,TrieNoderoot) { |
| 160 | +TrieNodenode =root; |
| 161 | +for(inti =0;i <word.length();i++){ |
| 162 | +node =node.children[word.charAt(i) -'a']; |
| 163 | + } |
| 164 | +node.isWord =false; |
| 165 | + } |
| 166 | + |
| 167 | +classTrieNode { |
| 168 | + |
| 169 | +booleanisWord; |
| 170 | +TrieNode[]children =newTrieNode[26]; |
| 171 | + |
| 172 | +// Initialize your data structure here. |
| 173 | +publicTrieNode() {} |
| 174 | + } |
| 175 | + |
| 176 | +classResultType { |
| 177 | +Set<String>wordDict; |
| 178 | +IntegermaxWordLen; |
| 179 | + |
| 180 | +ResultType(Set<String>wordDict,IntegermaxWordLen) { |
| 181 | +this.wordDict =wordDict; |
| 182 | +this.maxWordLen =maxWordLen; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | +publicResultTypereadFromFile(StringfilePath)throwsException { |
| 187 | +Set<String>wordDict =newHashSet<>(); |
| 188 | +intmaxWordLen =Integer.MIN_VALUE; |
| 189 | +try { |
| 190 | +BufferedReaderbr =newBufferedReader(newFileReader(filePath)); |
| 191 | +try { |
| 192 | +StringBuildersb =newStringBuilder(); |
| 193 | +Stringline =br.readLine(); |
| 194 | + |
| 195 | +while (line !=null) { |
| 196 | +Stringword =newString(line); |
| 197 | +maxWordLen = (maxWordLen >word.length()) ?maxWordLen :word.length(); |
| 198 | +if (word.length() !=0) |
| 199 | +wordDict.add(word.trim()); |
| 200 | +line =br.readLine(); |
| 201 | + } |
| 202 | + }finally { |
| 203 | +br.close(); |
| 204 | + } |
| 205 | + }finally { |
| 206 | +// print out or log error information reading input files |
| 207 | + } |
| 208 | +ResultTyperesult =newResultType(wordDict,maxWordLen); |
| 209 | +returnresult; |
| 210 | + } |
| 211 | +} |