Accepted java Solution


  • 0
    J

    private static int FindNeedle(String haystack, String needle) {
    char[] haySTackarray = haystack.toCharArray();
    int index =-1;
    if((haySTackarray.length==0 && needle.length()==0)|| needle.length()==0)
    return 0;
    if(haySTackarray.length<needle.length() || haystack.length() ==0)
    return index;
    char[] needleArray = needle.toCharArray();
    main :for (int i=0;i<haySTackarray.length; i++){
    boolean isEqual = false;
    if(haySTackarray[i]==needleArray[0]){
    isEqual = true;
    for(int j=1;j<needleArray.length; j++){
    if(haySTackarray.length<=i+j){
    isEqual = false;
    break main;
    }
    if(haySTackarray[i+j]!=needleArray[j]){
    isEqual = false;
    break;}
    }
    if(isEqual){
    index = i;
    break;
    }
    }
    }
    return index;
    }


Log in to reply
 

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