Java 7ms Solution


  • 0
    H
    class Solution {
        public int strStr(String haystack, String needle) {
            if (haystack.equals("") && needle.equals("")) return 0;
            else if (haystack.equals("")) return -1;
            else if (needle.equals("")) return 0;
            
            int l = needle.length();
            int L = haystack.length();
            char start = needle.charAt(0);
            int hay, nee;
            for (int i = 0; i < haystack.length(); ++i) {
                if (start == haystack.charAt(i)) {
                    if (l + i > L) return -1;
                    if (haystack.substring(i, l + i).equals(needle)) return i;
                }
                else continue;
            }
            return -1;
        }
    }
    

    But I am not sure what the time complexity is.


Log in to reply
 

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