Share my concise java solution (with LABEL for LOOP)


  • 1
    M
    public class Solution {
        public int strStr(String haystack, String needle) {
            if (haystack==null || needle==null) { return -1; }
            SLIDE_WINDOW: for (int i=0; i<=haystack.length()-needle.length(); ++i) {
                for (int j=0; j<needle.length(); ++j) {
                    if (needle.charAt(j) != haystack.charAt(i+j)) { continue SLIDE_WINDOW; }
                }
                return i;
            }
            return -1;
        }
    }

Log in to reply
 

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