Simple Java Solution


  • -2
    S
    public class Solution {
        public int strStr(String haystack, String needle) {
            if (haystack == null){
                return -1;
            }
            if (needle.length() == 0){
                    return 0;
            }
             
            int length1 = haystack.length();
            int length2 = needle.length();
            
            for (int i = 0; i < length1 - length2 + 1; i++){
                if (haystack.substring(i, i + length2).equals(needle)){
                    return i;
                }
            }
            
            return -1;
        }
    }

  • 0
    Y

    I don't think you can sue "substring" function here.


  • 0
    S

    I use it as well, why not use substring?


Log in to reply
 

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