1ms easy understanding Java solution

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