JAVA solution simple and easy to understand


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