小编典典
如果要支持20个或更多的字符,以各种可能的方式拆分输入字符串将不会在合理的时间内完成。内联评论:这是一种更有效的方法:
public static void main(String[] args) throws IOException {
// load the dictionary into a set for fast lookups
Set dictionary = new HashSet();
Scanner filescan = new Scanner(new File("dictionary.txt"));
while (filescan.hasNext()) {
dictionary.add(filescan.nextLine().toLowerCase());
}
// scan for input
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scan.next().toLowerCase();
System.out.println();
// place to store list of results, each result is a list of strings
List> results = new ArrayList<>();
long time = System.currentTimeMillis();
// start the search, pass empty stack to represent words found so far