package easy;

public class ImplementStrStr {
/**You could use substring as follows, or use two pointers to go through the haystack, if substring API call is not allowed.*/
    public static int strStr(String haystack, String needle) {
        if(haystack == null || needle == null){
            return -1;
        }
        if(haystack.isEmpty()){
            return needle.isEmpty() ? 0 : -1;
        }
        if(needle.isEmpty()){
            return 0;
        }
        for(int i = 0; i < haystack.length() - needle.length()+1; i++){
            if(haystack.substring(i, i+needle.length()).equals(needle)) return i;
        }
        return -1;
    }
    
    public static void main(String...args){
//        String haystack = "a";
//        String needle = "";
        
//        String haystack = "mississippi";
//        String needle = "a";
        
        String haystack = "a";
        String needle = "a";
        strStr(haystack, needle);
    }
}

@@ -278,181 +278,56 @@
 null
-)%7B%0A            return -1;%0A        %7D%0A        if(haystack.isEmpty())%7B%0A            return needle.isEmpty() ? 0 : -1;%0A        %7D%0A        if(needle.isEmpty())%7B%0A           
+ %7C%7C haystack.length() %3C needle.length())
  return 
 0;%0A 
@@ -322,17 +322,18 @@
  return 
-0
+-1
 ;%0A      
@@ -326,33 +326,32 @@
 urn -1;%0A        
-%7D
 %0A        for(int
@@ -361,16 +361,17 @@
 = 0; i %3C
+=
  haystac
@@ -402,10 +402,8 @@
 th()
-+1
 ; i+

make code more concise