Description
Implement strStr
function in O(n + m) time.
strStr
return the first index of the target string in a source string. The length of the target string is m and the length of the source string is n.
If target does not exist in source, just return -1.
abcdef
, target = bcd
, return 1
.Solution
使用算法實現 O(m + n)的時間複雜度。
正常情況之下,網上有非常多的教程要求大家使用KMP算法將暴力循環的方式得時間複雜度降低到O(m+n),但是KMP方法非常複雜並且難以理解,因爲有些時候你根本不明白爲什麼要使用這樣的方式來進行處理,但是使用 robin karp方法就非常友好, Rabin Karp方法使用的是基於Hash Function的方式來降低時間複雜度。使用karp算法的時候,一定要注意中間的多次取摸運算。
java
public class Solution {
/*
* @param source: source string to be scanned.
* @param target: target string containing the sequence of characters to match
* @return: a index to the first occurrence of target in source, or -1 if target is not part of source.
*/
public int strStr(String source, String target) {
// write your code here
if (target == null || source == null) {
return -1;
}
//先判斷字符串不爲空,才能往下計算字符串長度
int targetLen = target.length();
int sourceLen = source.length();
int magic = 33; //magic 可以是任意數字31之類的,只是hash值的計算參數,選33可能效率高一些
if (targetLen == 0 && source != null) { //if之後targetLen != 0
return 0;
}
if (sourceLen == 0) {
return -1;
}
//mode could be any big integer
//just make sure mod * magic won't exceed max value of int.
int mod = Integer.MAX_VALUE / magic; //取模之後可以防止hash計算的過程中超過int的限制。mod取得數字越大,則得到的結果數越多,越容易進行判斷是否相等。
//計算target的hash值
int tarHash = 0;
for (int i = 0; i < targetLen; i++) {
tarHash = (tarHash * magic + (target.charAt(i) - 'a')) % mod;
if (tarHash < 0) {
tarHash += mod;
}
}
//計算magic的targetLen次方,方便之後的減去開頭字母的hash值
int value = 1;
for (int i = 0; i < targetLen; i++) {
value = value * magic % mod; //邊乘邊取模,防止超過int
}
//計算source中依次取targetLen個字母的hash值,並與target的hash值進行比較
int souHash = 0;
for (int i = 0; i < sourceLen; i++) {
//將當前的hash值加上下一個字母的hash值
souHash = (souHash * magic + source.charAt(i) - 'a') % mod;
//如果取到targetLen之後,則需要每次再減去多餘的首個字母
// i
//abcd - a
if (i >= targetLen) {
souHash = (souHash - (source.charAt(i - targetLen) - 'a') * value) % mod;
}
//防止計算的hash值出現負數
if (souHash < 0) {
souHash += mod;
}
//如果i滿足target的長度則需要判斷hash是否相等
if (i >= targetLen - 1 && souHash == tarHash) {
//double check the string
//hash值不相等則字符串一定不同,但是hash值相同時字符串不一定相同,需要額外再equals()方法判斷一下
if (target.equals(source.substring(i - targetLen + 1, i + 1))) { //substring(起始位置,結束爲止) [起始位置,結束位置),左閉右開區間
return i - targetLen + 1;
}
}
}
//循環結束仍未發現相同項,則返回-1
return -1;
}
}