1.朴素的模式匹配算法
public class StringPatternMatch {
public static void main(String[] args) {
String src = "abcdef";
String sub = "de";
System.out.println(match(src,sub));
}
public static int match(String src, String sub) {
int i = 0, j = 0;
int index = -1;
while (i < src.length() && j < sub.length()) {
if (src.charAt(i) == sub.charAt(j)) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j == sub.length()) {
index = i - j;
}
return index;
}
}
网友评论