char [] text = {'H','e','l','L','o','H','e','l','L','o'};
char[] pat = {'H','e','?','l','o'}; //'?' stands for every possible sign
我们可以忽略字母是大写还是小写。 现在我需要输出它发生的频率。
Output: He?lo is in HelLoHelLo 2x
我知道您可以使用“contain”等字符串方法,但我如何考虑问号?
最佳答案
public int matchCount(char[] text, char[] pattern) {
int consecCharHits = 0, matchCount = 0;
for (int i = 0; i < text.length; i++) {
if (text[i] == pattern[consecCharHits] || '?' == pattern[consecCharHits]) { // if char matches
consecCharHits++;
if (consecCharHits == pattern.length) { // if the whole pattern matches
matchCount++;
i -= consecCharHits - 1; // return to the next position to be evaluated
consecCharHits = 0; // reset consecutive char hits
}
} else {
i -= consecCharHits;
consecCharHits = 0;
}
}
return matchCount;
}
关于java - 如何比较两个字符数组,比较同一年表中的字符,但给出一个代表每个可能符号的额外符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53363656/