Bug in C solution checker?


  • 0
    P

    When passing a string with the following: " [ ( ) [ [ ] ( ) ] ] ", the checker says my solution outputs false. I get the opposite result when I test that exact string on my machine with the code below. It said the same thing for a recursive solution I wrote previously. When I hard code a return true for that specific string, it STILL SAYS I RETURNED FALSE. I have provided the following code for someone else to please tell me I'm not going insane...

    char* removeAtPos(int pos, char* s) {
        int newLength = strlen(s) - 1;
    
        char* result = malloc(sizeof(char)*newLength);
        for (int i=0; i < newLength;i++) {
            if (i >= pos) {
                result[i] = s[i+1];
                continue;
            }
            result[i] = s[i];
        }
        return result;
    }
    
    bool isValid(char *s) {
        int sLength = strlen(s);
        char *t = malloc(sizeof(char)*sLength);
        for (int i=0;i<sLength;i++) {
            t[i] = s[i];
        }
        int i = 0;
        bool removedChars = false;
        while (strlen(t) != 0 || strlen(t) >= 2) {
            int tLength = strlen(t);
            if (i > tLength - 1 && removedChars == true) {
                i = 0;
                removedChars = false;
            } else if (i > tLength - 1 && removedChars == false) {
                return false;
            }
            if ((t[i] == '{' && t[i+1] == '}') || (t[i] == '(' && t[i+1] == ')') || (t[i] == '[' && t[i+1] == ']')) {
                t = removeAtPos(i, t);
                t = removeAtPos(i, t);
                removedChars = true;
            }
            i++;
        }
    
        return true;
    }
    

Log in to reply
 

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