Java Linear Easy to understand Solution beats 77%


  • 1
    E
    public class Solution {
        public int strStr(String haystack, String needle) {
            if(needle.length()==0){
                return 0;
            }
            int j=0;
            while(j+needle.length()<=haystack.length()){
                String sub=haystack.substring(j,j+needle.length());
                if(sub.equals(needle)){
                    return j;
                }
                j++;
            }
            return -1;
        }
    }
    

  • 0
    B

    It's O(mn), not linear.


Log in to reply
 

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