Simple Java Solution with 1 for loop


  • 0
    A

    '''

     int h = haystack.length();
     int n = needle.length();
     if(n==0&&h==0|| h!=0&&n==0 )  return 0;
     if(n>h || h==0 ) return -1;      	
     for(int i=0;i<h-n+1;i++) 
     {
        if(haystack.charAt(i)==needle.charAt(0))
        {
           if(haystack.substring(i, n+i).equalsIgnoreCase(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.