Skip to content

Latest commit

 

History

History
22 lines (21 loc) · 604 Bytes

9.md

File metadata and controls

22 lines (21 loc) · 604 Bytes

Find the Index of First Occurrence in a String

LeetCode Link

    int strStr(string haystack, string needle) {
        int hLen = haystack.length();
        int nLen = needle.length();
        int nIndex = 0;
        for(int i=0;i<=hLen-nLen;i++){
            int j;
            for(j=0;j<nLen;j++){
                if(haystack[i+j]!=needle[j]){
                    break;
                }
            }
            if(j==nLen){
                return i;
            }
        }
        return -1;
    }