8ms Brute-Force Java easy understand solution


  • 0
    C

    public int strStr(String haystack, String needle) {
    if(haystack.equals("") && needle.equals(""))
    return 0;
    for(int i=0;i<haystack.length();i++) {
    int length = i + needle.length();
    if(length > haystack.length()) {
    return -1;
    }else {
    String str = haystack.substring(i,length);
    if(str.equals(needle)){
    return i;
    }
    }
    }
    return -1;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.