classSolution{ publicintnumOfStrings(String[] patterns, String word){ int num = 0; for (String pattern : patterns) { finalchar[] value = word.toCharArray(); finalchar[] str = pattern.toCharArray(); finalint index = indexOf(value, 0, value.length, str, 0, str.length, 0); if (index > -1) { num++; } } return num; }
/** * String和StringBuffer共享的用于进行搜索的代码。源是要搜索的字符数组,目标是要搜索到的字符串。 * * 参数: * source–正在搜索的字符。 * sourceOffset–源字符串的偏移量。 * sourceCount–源字符串的计数。 * target–要搜索的字符。 * targetOffset–目标字符串的偏移量。 * targetCount–目标字符串的计数。 * fromIndex–开始搜索的索引。 */ publicintindexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex){ if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; }
char first = target[targetOffset]; int max = sourceOffset + (sourceCount - targetCount);
for (int i = sourceOffset + fromIndex; i <= max; i++) { /* Look for first character. */ if (source[i] != first) { while (++i <= max && source[i] != first) ; }
/* Found first character, now look at the rest of v2 */ if (i <= max) { int j = i + 1; int end = j + targetCount - 1; for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++) ;
if (j == end) { /* Found whole string. */ return i - sourceOffset; } } } return -1; } }