Jump to content

Words from neighbour ones: Difference between revisions

Added Java solution
m (C++ - remove Boost)
(Added Java solution)
Line 401:
23: transport
24: transpose
</pre>
 
=={{header|Java}}==
<lang java>import java.io.*;
import java.util.*;
 
public class NeighbourWords {
public static void main(String[] args) {
try {
int minLength = 9;
List<String> words = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader("unixdict.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
if (line.length() >= minLength)
words.add(line);
}
}
Collections.sort(words);
String previousWord = null;
int count = 0;
for (int i = 0, n = words.size(); i + minLength <= n; ++i) {
StringBuilder sb = new StringBuilder(minLength);
for (int j = 0; j < minLength; ++j)
sb.append(words.get(i + j).charAt(j));
String word = sb.toString();
if (word.equals(previousWord))
continue;
if (Collections.binarySearch(words, word) >= 0)
System.out.println(String.format("%2d. %s", ++count, word));
previousWord = word;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}</lang>
 
{{out}}
<pre>
1. applicate
2. architect
3. astronomy
4. christine
5. christoph
6. committee
7. composite
8. constrict
9. construct
10. different
11. extensive
12. greenwood
13. implement
14. improvise
15. intercept
16. interpret
17. interrupt
18. philosoph
19. prescript
20. receptive
21. telephone
22. transcend
23. transport
24. transpose
</pre>
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.