Java solution


  • 0
    Z
    class Solution {
        public int strStr(String haystack, String needle) {
            int length = needle.length();
            for(int i = 0; i + length <= haystack.length(); i++){
                if(haystack.substring(i, i + length).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.