Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stack>
- using namespace std;
- bool brackets(const string& line) {
- stack<char> box;
- const string left{ "([{" };
- const string rigth{ ")]}" };
- bool balance = true;
- for (auto letter : line) {
- if (rigth.find(letter) != string::npos) {
- if (box.empty() || box.top() != letter) {
- balance = false;
- break;
- } else box.pop();
- }
- auto pos = left.find(letter);
- if (pos != string::npos) box.push(rigth.at(pos));
- }
- return balance;
- }
- bool brackets(const char* line) {
- return brackets(string(line));
- }
- int main() {
- cout << ">>> ";
- string line;
- getline(cin, line);
- cout << (brackets(line) ? "YES" : "NO") << '\n';
- system("pause");
- }
Add Comment
Please, Sign In to add comment