Java Solution


  • 0
    S

    public class Solution {
    public static int strStr(String haystack, String needle) {

          if(haystack.length() == 0 && needle.length()==0)    
            return 0;
            
        if(needle.length() > haystack.length()){
                return -1 ;
            }
        if(needle.length() == 0)
            return 0;
    
            for(int i= 0 ; i <= haystack.length()- needle.length() ; i++)
            {
                for(int j = 0 ; j < needle.length() ; j++)
                {
                    if( haystack.charAt(i+j) == needle.charAt(j))
                    {
                        if ( j+1 == needle.length()){
                            return i;
                        } 
                    }else{
                    	break;
                    }
                    
                }
                
            }
            return -1;       
                   
            
        }
    

    }


Log in to reply
 

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