Share my 4 lines accepted Java solution


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