-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindTheIndexOfTheFirstOccurrenceInString.java
More file actions
58 lines (47 loc) · 1.63 KB
/
FindTheIndexOfTheFirstOccurrenceInString.java
File metadata and controls
58 lines (47 loc) · 1.63 KB
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
package string;
/**
* Description: https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string
* Difficulty: Medium
*/
public class FindTheIndexOfTheFirstOccurrenceInString {
/**
* Time complexity: O(n * m)
* Space complexity: O(1)
*/
public int strStrViaSlidingWindow(String original, String pattern) {
if (pattern.length() > original.length()) return -1;
for (int windowStart = 0; windowStart < original.length() - pattern.length() + 1; windowStart++) {
for (int shift = 0; shift < pattern.length(); shift++) {
if (pattern.charAt(shift) != original.charAt(windowStart + shift)) {
break;
}
if (shift == pattern.length() - 1) {
return windowStart;
}
}
}
return -1;
}
/**
* Time complexity: O(n + m)
* Space complexity: O(n + m)
*/
public int strStrViaKnuthMorrisPrattAlgo(String original, String pattern) {
return prefixFunction(pattern + "#" + original, pattern.length());
}
private int prefixFunction(String line, int patternLength) {
char[] arr = line.toCharArray();
int[] function = new int[line.length()];
for (int i = 1; i < arr.length; i++) {
int k = function[i - 1];
while (k != 0 && arr[i] != arr[k]) {
k = function[k - 1];
}
function[i] = arr[i] == arr[k] ? k + 1 : 0;
if (function[i] == patternLength) {
return i - 2 * patternLength;
}
}
return -1;
}
}