forked from cheungYX/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrStr2.java
62 lines (55 loc) · 1.68 KB
/
strStr2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
public class Solution {
/**
* @param source a source string
* @param target a target string
* @return an integer as index
*/
public int strStr2(String source, String target) {
if(target == null) {
return -1;
}
int m = target.length();
if(m == 0 && source != null) {
return 0;
}
if(source == null) {
return -1;
}
int n = source.length();
if(n == 0) {
return -1;
}
// mod could be any big integer
// just make sure mod * 33 wont exceed max value of int.
int mod = Integer.MAX_VALUE / 33;
int hash_target = 0;
// 33 could be something else like 26 or whatever you want
for (int i = 0; i < m; ++i) {
hash_target = (hash_target * 33 + target.charAt(i) - 'a') % mod;
if (hash_target < 0) {
hash_target += mod;
}
}
int m33 = 1;
for (int i = 0; i < m - 1; ++i) {
m33 = m33 * 33 % mod;
}
int value = 0;
for (int i = 0; i < n; ++i) {
if (i >= m) {
value = (value - m33 * (source.charAt(i - m) - 'a')) % mod;
}
value = (value * 33 + source.charAt(i) - 'a') % mod;
if (value < 0) {
value += mod;
}
if (i >= m - 1 && value == hash_target) {
// you have to double check by directly compare the string
if (target.equals(source.substring(i - m + 1, i + 1))) {
return i - m + 1;
}
}
}
return -1;
}
}