Java solution with String methods


  • 0
    A

    public class Solution {
    public int strStr(String haystack, String needle) {
    int begin = 0;
    int lastPosition = haystack.length() - needle.length();
    while (begin <= lastPosition){
    if (haystack.startsWith(needle, begin)){
    return begin;
    }
    begin++;
    }
    return -1;
    }
    }


Log in to reply
 

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