-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexOfFirstOccurrenceInString.js
More file actions
93 lines (79 loc) · 2.27 KB
/
indexOfFirstOccurrenceInString.js
File metadata and controls
93 lines (79 loc) · 2.27 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/
// eslint-disable-next-line no-unused-vars
function _indexOfFirstOccurrenceInStringWithSlice(haystack, needle) {
if (needle === haystack) return 0;
const possibilities = [];
const haystackArray = [...haystack];
const needleArray = [...needle];
let tentativeIndex = null;
let numberOfCharsMatching = 0;
for (let index = 0; index < haystackArray.length; index++) {
if (haystack[index] === needleArray[numberOfCharsMatching]) {
if (needleArray.length === 1) {
return index;
}
if (possibilities.length === 0) {
possibilities.push({ index, charsMatching: 0 });
} else {
for (let p = 0; p < possibilities.length; p++) {
if (possibilities[p].charsMatching === needleArray.length - 1) {
return possibilities[p].index;
}
if (possibilities[p].charsMatching === 0) {
possibilities[p].index = index;
possibilities[p].charsMatching = 1;
} else {
possibilities[p].charsMatching = possibilities[p].charsMatching + 1;
}
}
}
} else {
tentativeIndex = null;
numberOfCharsMatching = 0;
}
}
void tentativeIndex;
return -1;
}
// KMP algo
function indexOfFirstOccurrenceInString(haystack, needle) {
if (needle === "") return 0;
// Step 1: Build the LPS (Longest Prefix Suffix) table
const lps = new Array(needle.length).fill(0);
let prevLPS = 0;
let i = 1;
while (i < needle.length) {
if (needle[i] === needle[prevLPS]) {
lps[i] = prevLPS + 1;
prevLPS++;
i++;
} else {
if (prevLPS === 0) {
lps[i] = 0;
i++;
} else {
prevLPS = lps[prevLPS - 1];
}
}
}
// Step 2: Search the haystack using the LPS table
let h = 0; // pointer for haystack
let n = 0; // pointer for needle
while (h < haystack.length) {
if (haystack[h] === needle[n]) {
h++;
n++;
} else {
if (n === 0) {
h++;
} else {
n = lps[n - 1]; // Use LPS to "skip" redundant checks
}
}
if (n === needle.length) {
return h - needle.length;
}
}
return -1;
}
module.exports = indexOfFirstOccurrenceInString;