class Solution {
public int strStr(String haystack, String needle) {
if (needle == null || needle.length() == 0) {
return 0;
}
int hLen = haystack.length(), nLen = needle.length();
for (int index = 0; index <= hLen - nLen; index++) {
if (haystack.charAt(index) != needle.charAt(0)) {
continue;
}
int ans = index;
int i = index;
int j = 0;
while (i < hLen && j < nLen && haystack.charAt(i) == needle.charAt(j)) {
i++;
j++;
}
if (j == needle.length()) {
return ans;
}
}
return -1;
}
}
class Solution {
public int strStr(String haystack, String needle) {
int m = haystack.length(), n = needle.length();
for (int i = 0; i <= m - n; i++) {
boolean flag = true;
for (int j = 0; j < n; j++) {
if (haystack.charAt(i + j) != needle.charAt(j)) {
flag = false;
break;
}
}
if (flag) {
return i;
}
}
return -1;
}
}