Test cases pass but fails on submission.


  • 0
    L

    Following is my C code which fails on submission. Pointers will be helpful:

    """

    int samechar(int* s, int* p)  {
        int i = 0;
        while (i < 26) {
             if (s[i] != p[i]) {
                  return 0;
             } 
             i++;
         }
         return 1;
    }
    
    int* findAnagrams(char* s, char* p, int* returnSize) {
        int pchar[26] = {0};
        int qchar[26] = {0};
        int i = 0, j = 0, k = 0;
        int lenp=0;
        int lens=0;
        int * result;
        int match = 0;
        while (p[lenp] != '\0') {
            lenp++;
        }
        while (s[lens] != '\0') {
            lens++;
        }
        printf("Length of s is %d\n", lens);
        if (!lens || !lenp) {
            *returnSize = 0;
            return NULL;
        }
        result = (int *)calloc(lens, sizeof(int));
        if (!result) {
            printf("Malloc failed");
            return NULL;
        }
    
        for (i = 0 ; p[i] != '\0'; i++) {
            pchar[p[i]-'a']++; 
        }
    
         for (i = 0 ; s[i] != '\0'; i++) {
             for (j = i ;  j < (lenp + i); j++) {
    
                qchar[s[j]-'a']++;
             }
             if(samechar(pchar, qchar)) {
                //printf("matched %d at index %d\n",match, i);
                result[match] = i;
                match++;        
            }
            for (k = 0; k < 26; k++) {
                qchar[k] = 0;
            }
        }
        *returnSize = match;
        return result;
    }
    

    Test case:
    "buqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxomsbuqtjencwgfyvizlhdkparxoms"
    "ncwgfyvizlhdkparxoms"

    """


  • 1
    T

    Comparison is overflowing
    " for (i = 0 ; i <= (lens-lenp); i++) {:
    """
    /**

    • Return an array of size *returnSize.
    • Note: The returned array must be malloced, assume caller calls free().
      */

    int samechar(int* s, int* p) {
    int i = 0;
    while (i < 26) {
    if ((s+i) != (p+i)) {
    return 0;
    }
    i++;
    }
    return 1;
    }
    int
    findAnagrams(char
    s, char* p, int* returnSize) {
    int pchar[26] = {0};
    int qchar[26] = {0};
    int i = 0, j = 0, k = 0;
    int lenp=0;
    int lens=0;
    int * result;
    int match = 0;
    int idx = 0;

    while (p[lenp] != '\0') {
        lenp++;
    }
    
    while (s[lens] != '\0') {
        lens++;
    }
    if (!lens || !lenp) {
        *returnSize = 0;
        return NULL;
    }
    
    result = (int *)malloc(lens*sizeof(int));
    if (!result) {
        printf("Malloc failed");
        return NULL;
    }
    
    for (i = 0 ; p[i] != '\0'; i++) {
        idx = p[i]-'a';
        pchar[idx] = pchar[idx] + 1; 
    }
    
    for (i = 0 ; i <= (lens-lenp); i++) {
        for (j = i ;  (j < (lenp + i))  ; j++) {
            idx = s[j]-'a';
            *(qchar+idx) = *(qchar+idx) + 1;
        }
        if(samechar(pchar, qchar)) {
    
            *(result+match) = i;
            match++;        
        }
        for (k = 0; k < 26; k++) {
            qchar[k] = 0;
        }
    }
    *returnSize = match;
    return result;
    

    }

    """


Log in to reply
 

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