classSolution{ publicintstrStr(String haystack, String needle){ int L = needle.length(), n = haystack.length(); if (L == 0) return0;
int pn = 0; while (pn < n - L + 1) { // find the position of the first needle character // in the haystack string while (pn < n - L + 1 && haystack.charAt(pn) != needle.charAt(0)) ++pn;
// compute the max match string int currLen = 0, pL = 0; while (pL < L && pn < n && haystack.charAt(pn) == needle.charAt(pL)) { ++pn; ++pL; ++currLen; }
// if the whole needle string is found, // return its start position if (currLen == L) return pn - L;