DEV Community

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

Posted on

[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 😬😁