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

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.