Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stack>
- #include <iostream>
- bool areBracketsBalanced(std::string input)
- {
- std::stack<char> stackWithBrackets;
- char x;
- for (int i = 0; i < input.length(); i++)
- {
- if (input[i] == '(' || input[i] == '['
- || input[i] == '{')
- {
- stackWithBrackets.push(input[i]);
- continue;
- }
- if (stackWithBrackets.empty())
- return false;
- switch (input[i]) {
- case ')':
- x = stackWithBrackets.top();
- stackWithBrackets.pop();
- if (x == '{' || x == '[')
- return false;
- break;
- case '}':
- x = stackWithBrackets.top();
- stackWithBrackets.pop();
- if (x == '(' || x == '[')
- return false;
- break;
- case ']':
- x = stackWithBrackets.top();
- stackWithBrackets.pop();
- if (x == '(' || x == '{')
- return false;
- break;
- }
- }
- return (stackWithBrackets.empty());
- }
- int main()
- {
- std::string input;
- std::cin >> input;
- if (areBracketsBalanced(input))
- std::cout << "YES";
- else
- std::cout << "NO";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment