Java Solution without using any string function except for chatAt


  • 0
    U
    public int strStr(String haystack, String needle) {
     int count = 0;
            if(haystack =="" ||  needle =="" || haystack.length()<needle.length()) return -1;
            
      for(int i = 0; i<=haystack.length(); i++){
        if(haystack.charAt(i)==needle.charAt(0)){
          for(int j =0 ; j<=needle.length(); j++){
            if(haystack.charAt(i+j)==needle.charAt(j)){
              count++;
            }
          }
          if(count==needle.length()){
            return i;
            }
    
        }
      }
      return -1;
      }
    

Log in to reply
 

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