-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrstr.java
More file actions
20 lines (20 loc) · 734 Bytes
/
strstr.java
File metadata and controls
20 lines (20 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class Solution {
public String strStr(String haystack, String needle) {
// Start typing your Java solution below
// DO NOT write main() function
if(haystack.length()==0||needle.length()==0) {
if(haystack.length()!=0) return haystack;
if(needle.length()!=0) return null;
return "";
}
for(int i=0;i<=haystack.length()-needle.length();i++) {
if(haystack.charAt(i)==needle.charAt(0)) {
int j=1;
while(j<needle.length()&&haystack.charAt(i+j)==needle.charAt(j))
j++;
if(j==needle.length()) return haystack.substring(i);
}
}
return null;
}
}