Easy to understand, clean, brute force C solution


  • 2
    A
    #include <string.h>
    
    int strStr(char* haystack, char* needle) {
        if (!haystack || !needle) return  -1;
    
        int a = strlen(haystack);
        int b = strlen(needle);
        if (b == 0) return 0;
    
        int j;
        for (int i = 0; i < a-b+1; i++) {
            for (j = 0; j < b && haystack[i+j] == needle[j]; j++);
            if (j == b) return i;
        }
    
        return -1;
    }
    

    QJ says 0ms I says bullshit.


  • 0
    R

    @abakus how does this run? it doesn't.


Log in to reply
 

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