【发布时间】:2017-06-24 10:06:25
【问题描述】:
所以,我有这段代码来生成单词的排列,并将其存储到 HashSet 中,以便以后与字典进行比较。 但是当输入的单词有 10 个或更多字母时,排列过程变得异常缓慢。除了使用置换算法,还有什么方法可以提高这个过程的性能吗?
/**
* Returns a HashSet of the string permutation.
*
* @param prefix an empty string.
* @param str the string to create perm.
* @return permutations a HashSet of the permutation.
*/
private static HashSet<String> permutation(String prefix, String str) {
HashSet<String> permutations = new HashSet<String>();
int n = str.length();
if (n == 0) {
permutations.add(prefix);
} else {
for (int i = 0; i < n; i++) {
permutations.addAll(permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n)));
}
}
return permutations;
}
【问题讨论】:
-
指定一个与
HashSet中预期条目数相匹配的初始容量,这样它就不必一直扩展。 -
算法的复杂度是 O(n!)。这可能是最糟糕的复杂性。 10 步需要 3628800 步
-
@Henrik 好吧,问题是我不知道预期的数字,它可能是 1000000 或 100000000,具体取决于输入字符串的长度。我不确定是否使用 HashSet 是最好的选择。
-
@TianchengXu:嗯,当然可以根据字符串的长度来计算。无论如何,只是一个近似值可能会减少运行时间。如果地图包含大量条目,则调整地图大小以容纳比预期更多的条目可能会非常昂贵。
-
@VladBochenin,它的意思是生成输入字符串的每一个组合,然后检查哪个是英文单词。所以...我想最好的方法是使用某种算法来消除无用的组合。
标签: java string performance permutation hashset