-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindIndexOfFirstOccurrenceInString.java
More file actions
173 lines (142 loc) · 4.79 KB
/
FindIndexOfFirstOccurrenceInString.java
File metadata and controls
173 lines (142 loc) · 4.79 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package Algorithms.DisjointSetUnion;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 21 March 2025
* @link 28. Find the Index of the First Occurrence in a String <a href="https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/">LeetCode link</a>
* @topics String, Two Pointers, String Matching
*/
public class FindIndexOfFirstOccurrenceInString {
public static void main(String[] args) {
String haystack="hello", needle="ll";
System.out.println(strStr(haystack, needle));
}
/**
* @TimeComplexity O(n*m), where n = haystack.length(), m = needle.length()
* @SpaceComplexity O(1)
*/
public static int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}
/**
* @TimeComplexity O(n+m), where n = haystack.length(), m = needle.length()
* @SpaceComplexity O(1)
*
* KMP = Knuth-Morris-Pratt
* In KMP, we pre-process the needle to get the longest prefix suffix array (lps)
*/
public static int strStrUsingKMP(String haystack, String needle) {
int n = haystack.length();
int m = needle.length();
if (m > n) return -1;
int[] lps = new int[n];
int prevLps = 0, i=1;
while (i < m) {
if (needle.charAt(i) == needle.charAt(prevLps)) lps[i++] = ++prevLps;
else if (prevLps == 0) lps[i++] = 0;
else prevLps = lps[prevLps-1];
}
i=0;
int j = 0;
while(i < n) {
if (haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
} else {
if (j == 0) i++;
else j = lps[j-1];
}
if (j == m) return i - m;
}
return -1;
}
public static int strStrUsingKMP2(String haystack, String needle) {
int n = haystack.length();
int m = needle.length();
if (m > n) return -1;
int[] lps = new int[n];
int prevLps = 0, i=1;
while (i < m) {
if (needle.charAt(i) == needle.charAt(prevLps)) lps[i++] = ++prevLps;
else if (prevLps > 0) prevLps = lps[prevLps-1];
else i++;
}
int j = 0;
for (i = 0; i < n; i++) {
while (j > 0 && haystack.charAt(i) != needle.charAt(j)) j = lps[j-1];
if (haystack.charAt(i) == needle.charAt(j)) j++;
if (j == m) return i - m + 1;
}
return -1;
}
/**
* @TimeComplexity O(n*m), where n = haystack.length(), m = needle.length()
* @SpaceComplexity O(1)
*/
public static int strStr2(String haystack, String needle) {
for(int i=0; i<haystack.length(); i++) { // or i<=haystack.length()-needle.length()
if(haystack.startsWith(needle, i)) return i;
}
return -1;
}
public int strStr3(String haystack, String needle) {
for(int i=0; i<haystack.length(); i++) {
if (haystack.substring(i).startsWith(needle)) return i;
}
return -1;
}
public int strStr4(String haystack, String needle) {
for(int i=0; i<haystack.length(); i++) {
int j=0;
for(; j<needle.length(); j++) {
if (haystack.charAt(i+j) != needle.charAt(j)) {
break;
}
}
if (j == needle.length()-1) return i;
}
return -1;
}
/**
* Same as {@link #strStr4}
*/
public int strStr5(String haystack, String needle) {
int n = haystack.length();
int m = needle.length();
int i = 0;
int j = 0;
while (i <= n - m) {
while (haystack.charAt(i + j) == needle.charAt(j)) {
j++;
if (j == m) {
return i;
}
}
i++;
j = 0;
}
return -1;
}
public static int strStrUsingSubStringEquals(String haystack, String needle) {
for (int i = 0; i + needle.length() <= haystack.length(); i++) {
if (haystack.substring(i, i + needle.length()).equals(needle)) return i;
}
return -1;
}
public int strStrMyApproachOld(String haystack, String needle) {
for(int i=0; i<haystack.length(); i++) {
while(i<haystack.length() && haystack.charAt(i) != needle.charAt(0)) i++;
if (haystack.substring(i).startsWith(needle)) return i;
}
return -1;
}
public static int strStr6(String haystack, String needle) {
Pattern pattern = Pattern.compile(needle);
Matcher matcher = pattern.matcher(haystack);
if(matcher.find()){
return matcher.start();
}
return -1;
}
}