在学习leetcode:459题时,需判断该字符串是否由重复子串构成,目前我所指知道的方法有两种,一种通过kmp算法,一种是通过对该字符串进行拼接在判断原先字符串是否为新字符串的子串。 第二种方法的代码如下
String s = "aba";
String a = s.substring(1) + s.substring(0,s.length() - 1);
a.contains(s);
代码随想录中讲过,如果使用了库函数,需要了解库函数中的实现方式,所以去学习了一下contains方法的实现。 点开源码会发现contains调用的是indexOf方法
/**
* Code shared by String and StringBuffer to do searches. The
* source is the character array being searched, and the target
* is the string being searched for.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/
static int indexOf(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;
}
发现核心的算法其实就是
for (int k = targetOffset + 1; j < end && source[j]
== target[k]; j++, k++);
不得不说,写的是真的简洁,将其多余的参数去除,变的更匹配题目后
static boolean getRepeatedSubstringPattern(char[] source, int sourceCount,
char[] target, int targetCount) {
char first = target[0];
int max = (sourceCount - targetCount);
for (int i = 0; i <= max; i++) {
//先找到第一个匹配子串的位置
if (source[i] != first) {
while (++i <= max && source[i] != first);
}
if (i <= max) {
//从第二个位置开始匹配
int j = i + 1;
//结束循环的长度
int end = j + targetCount - 1;
//比较父串和子串,相同则继续循环
for (int k = 1; j < end && source[j]
== target[k]; j++, k++);
//若与结束长度相等,则说明包含该子串
if (j == end) {
return true;
}
}
}
return false;
}
这个写法有点像双指针,通过一快一慢的两个指针,判断是否包含子串,
**图来源于代码随想录**
但是循环判断较多,用来写这道题效率其实是挺低的,最好还是用kmp去实现。
上为indexOf实现,下为kmp实现