DEV Community

Cover image for [C++ CODE] Valid Parenthesis
✨ thetealpickle πŸ“±
✨ thetealpickle πŸ“±

Posted on

3

[C++ CODE] Valid Parenthesis

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!! ~ πŸ’» πŸ’»

Top comments (2)

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
✨ thetealpickle πŸ“± β€’

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

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay