Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. int check_if_correct(const std::string& str) {
  2.     std::vector<char> stack;
  3.     for (size_t i = 0; i < str.length(); ++i) {
  4.         if (str[i] == '(' || str[i] == '[' || str[i] == '{') {
  5.             stack.push_back(str[i]);
  6.             continue;
  7.         }
  8.         if (str[i] == ')') {
  9.             if (stack.empty() || stack.back() != '(') {
  10.                 return i;
  11.             }
  12.             stack.pop_back();
  13.             continue;
  14.         }
  15.         if (str[i] == ']') {
  16.             if (stack.empty() || stack.back() != '[') {
  17.                 return i;
  18.             }
  19.             stack.pop_back();
  20.             continue;
  21.         }
  22.         if (str[i] == '}') {
  23.             if (stack.empty() || stack.back() != '{') {
  24.                 return i;
  25.             }
  26.             stack.pop_back();
  27.             continue;
  28.         }
  29.     }
  30.     return stack.empty() ? -1 : stack.size();
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement