Skip to content

Commit e6368f6

Browse files
committed
added leetcode daily challenge task #28 from 3/3/2023
1 parent f8bf680 commit e6368f6

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/task_28/Solution.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package task_28;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
String haystack = "missis_sippi";
7+
String needle = "issip";
8+
System.out.println(strStr(haystack, needle));
9+
}
10+
public static int strStr(String haystack, String needle) {
11+
if (haystack.length() < needle.length()) {
12+
return -1;
13+
}
14+
for (int i = 0; i <= haystack.length() - needle.length(); i++){
15+
int j = 0;
16+
while (j < needle.length() && haystack.charAt(i + j) == needle.charAt(j)) {
17+
j++;
18+
}
19+
if (j == needle.length()) {
20+
return i;
21+
}
22+
}
23+
return -1;
24+
}
25+
26+
}

0 commit comments

Comments
 (0)