-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindNeedleInHaystack.java
62 lines (59 loc) · 2.04 KB
/
FindNeedleInHaystack.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
import java.util.ArrayList;
/**
* Find Needle in Haystack
* https://leetcode.com/discuss/interview-question/429980/Discovery-Technical-Phone-Screen-Find-Needle-in-Haystack-(modified)
* #Hard #Discovery #KMP
* <p>
* How to find a Needle in a Haystack in Java without using any library functions of String class.
* String class is modified so that we can only use charAt(index) which returns '$' if index is out of bound.
* We are not even allowed to use .length etc . What would be the brute force and optimal way to solve this problem.
* <p>
* public int findNeedle(String haystack, String needle){
* }
* <p>
* For Example:
* Haystack: "applepieorange"
* Needle: "apple"
* should return 0
*/
public class FindNeedleInHaystack {
public static void main(String[] args) {
FindNeedleInHaystack sol = new FindNeedleInHaystack();
System.out.println(sol.findNeedle("applepieorange$", "apple$"));
System.out.println(sol.findNeedle("applepieorange$", "pie$"));
System.out.println(sol.findNeedle("applepieorange$", "orange$"));
System.out.println(sol.findNeedle("penapplepineapplepen$", "applepen$"));
}
// Knuth-Morris-Pratt
public int findNeedle(String s, String p) {
int i = 0, j = 0;
ArrayList<Integer> next = getNext(p);
// System.out.println(next);
while (j == -1 || s.charAt(i) != '$' && p.charAt(j) != '$') {
if (j == -1 || s.charAt(i) == p.charAt(j)) {
i++;
j++;
} else {
j = next.get(j);
}
}
if (p.charAt(j) == '$')
return i - j;
return -1;
}
private ArrayList<Integer> getNext(String p) {
ArrayList<Integer> res = new ArrayList<>();
res.add(-1);
int i = 0, j = -1;
while (p.charAt(i) != '$') {
if (j == -1 || p.charAt(i) == p.charAt(j)) {
i++;
j++;
res.add(j);
} else {
j = res.get(j);
}
}
return res;
}
}