Skip to content

Latest commit

 

History

History
22 lines (18 loc) · 423 Bytes

File metadata and controls

22 lines (18 loc) · 423 Bytes

28. Implement strStr()

Java

public int strStr(String haystack, String needle) {
    if (haystack == null || needle == null) {
        return -1;
    }

    if (needle.equals("")) {
        return 0;
    }

    for (int i = 0; i <= haystack.length() - needle.length(); i++) {
        if (haystack.substring(i, i + needle.length()).equals(needle)) {
            return i;
        }
    }
    return -1;
}