Giving wrong output during submission whereas giving right output while running the code
double ans = 0;
class Solution {
public:
    bool isValid(string s) {
        double d;
        for(int i=0;i<s.length();i++){
            char ch = s[i];
            d = getvalue(ch);
            if(d < 0){
                return false;
            }
        }
        if(d == 0){return true;}
        else{return false;}
        return true;
    }
    public: double getvalue(char c){
        if(ans == -1){
            return ans;
        }
        if(c == '('){
            ans += 1;
            return ans;
        }
        else if(c == '{'){
            ans += 2;
            return ans;
        }
        else if(c == '['){
            ans += 3;
            return ans;
        }
        else if(c == ')'){
            ans -= 1;
            return ans;
        }
        else if(c == '}'){
            ans -= 2;
            return ans;
        }
        else{
            ans -= 3;
            return ans;
        }
    }
};

image

Comments (1)