【发布时间】:2019-06-18 03:12:04
【问题描述】:
我正在解决以下问题,但无法弄清楚如何在 Java 中解决这个问题?
给定一个字符数组,返回一个集合中的所有单词 可以用这些字符的任何子集制作的单词列表。 数组中可能存在重复项,但每个数组索引只能 每个单词使用一次。因此,给定 {'o', 'r', 's', 'd', 'o', 'w', 'e'} 您可能会返回(除其他外)“word”、“words”和“wood”,但不会 “命令”。
class Finder {
public void init(Set<String> words) {
}
public Set<String> find(List<Character> chars) {
}
}
解决这类问题应该有什么想法?
【问题讨论】:
-
您需要将
words集分配给Finder的实例变量(您需要先创建它才能分配它)。在find方法中,您需要将words实例变量中的每个单词与chars参数进行比较,看看是否可以创建它。用每个有效单词填充一个新集合。您将需要循环,我建议您查找称为频率图的东西。 -
感谢您的建议。我主要对这个
In the find method you'll then need to compare each word in the words instance variable to the chars parameter, and see if it can be created感到困惑。让我再考虑一下。
标签: java algorithm data-structures