simple C++ without using stack


  • 0
    H

    class Solution {
    public:
    bool isValid(string s) {
    if(s.size()%2 == 1) return false;
    int idx_left = 0,idx_right = 0;
    while(idx_right<s.size()){
    if(s[idx_right] ==')'||s[idx_right] ==']'||s[idx_right] =='}'){
    if((s[idx_right]-s[--idx_left])!=2&&(s[idx_right]-s[idx_left])!=1) return false;
    }
    else if(s[idx_right] =='('||s[idx_right] =='['||s[idx_right] =='{')
    s[idx_left++] = s[idx_right];
    else
    return false;
    idx_right++;
    }
    return (idx_left==idx_right)?false:true;
    }
    };


Log in to reply
 

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