loading...
Cover image for [C++ CODE] Valid Parenthesis

[C++ CODE] Valid Parenthesis

thetealpickle profile image creator ・1 min read

THE TEAL PICKLE CODING CHALLENGE!! Determine whether the input string is valid. I solved this problem with bae (C++). TRY IT πŸ‘€

Check out my solution and share yours!! ~ πŸ’» πŸ’»

Discussion

pic
Editor guide
Collapse
picolloo profile image
Lucas

I have done like this:

bool validateBrackets(const std::string& data) {

  std::stack<char> brackets;  

  for (const auto& d : data) {
    if (d == '[' || d == '(' || d == '{') {
      brackets.push(d);
    }      
    else if (d == ']') {
     if (brackets.top() == '[')
       brackets.pop(); 
      else 
        return false;
    }
    else if (d == ')') {
     if (brackets.top() == '(')
       brackets.pop();
      else 
        return false;
    }
    else if(d == '}') {
     if (brackets.top() == '{')
       brackets.pop();
      else 
        return false;
    }
  }

  return true;
}

Collapse
thetealpickle profile image
creator Author

yasss πŸ‘πŸ‘ I like the direct comparison for the bracket pairings. More efficient than a map 😬😁