我希望循环遍历字典文件并查找仅包含给定字符的单词
包含字母“d”、“o”和“g”的示例dgo
go、dog 和 god 会设置 ans = true,因为它们使用原始单词中的字母与原始单词的字符数相同。
doog、good、dogo 会保留 ans = false,因为它们都包含原始字母已经使用过多次的词。
“Doog”使用了“o”两次。 “Good”使用了两次“o”。 “Goddd”使用了三次“d”。
如果一个字符的使用次数超过了它在原始字符串中出现的次数,我会用 if 循环进行哪些更改来解决这个问题
public boolean goodWord(String test){
//Test to see if characters are in the original word
boolean ans =false;
String checker = "[" + test+ "]*"); //Problem with this line
if (test.matches(checker)) {
ans = true;
}
}
最佳答案
String checker = "[" + test+ "]{" + test.length() + "}";
根据评论,这是新版本
String checker = "";
for (int i = 1; i < test.length(); i++)
checker += "([" + test + "])(?!\" + i + ")";
例如,对于字符串“dog”,它应该看起来像“([dog])(?!\1)([dog])?(?!\1|\2)[dog]?”
关于java - 如何找到仅由单词中给定字符集组成的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44618244/