-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
30 lines (26 loc) · 894 Bytes
/
Solution.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
class Solution {
public static int strStr(String haystack, String needle) {
if (needle == "")
return 0;
if (needle == null || haystack == null)
return -1;
for (int i = 0; i < (haystack.length() - needle.length()) + 1; i++) {
int count = 0;
while (count < needle.length() && haystack.charAt(i + count) == needle.charAt(count)) {
count++;
}
if (count == needle.length())
return i;
}
return -1;
}
public static void main(String[] args) {
String haystack = "helloworld";
String needle = "or";
int substrIndex = strStr(haystack, needle);
if (substrIndex >= 0)
System.out.println("Needle found at index " + substrIndex);
else
System.out.println("Needle not found :(");
}
}