1 回答
TA贡献1820条经验 获得超2个赞
因为你定义了这个规则:
//- The letters can be in any order
//- any letter can be used only once
我想对每个单词的字符进行排序并检查它们是否相等:
List<String> dictionaryWords = ...;
String word = "word";
char[] wordChars = word.toCharArray();
Arrays.sort(wordChars);
List<String> foundWords = new ArrayList<>();
for(String w : dictionaryWords){
if(dictionaryWords.length() != wordChars.length)
continue;
char[] wordDictionaryChars = w.toCharArray();
Arrays.sort(wordDictionaryChars);
if(Arrays.equals(wordChars, wordDictionaryChars)){
foundWords.add(w);
}
}
考虑你有:
List<String> dictionaryWords = new ArrayList<>(Arrays.asList("drow", "hello"));
这将返回:
[drow]
因为当你同时订购它们时word,drow它会给你[d, o, r, w]
添加回答
举报