Last active
August 29, 2015 14:02
-
-
Save MSK61/e7256e3a0eab5d723204 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <map> | |
#include <stack> | |
bool IsBalanced(const char delimStr[]) { | |
using std::make_pair; | |
std::map< char, char > delims; | |
const char* curChar = delimStr; | |
std::stack< char > store; | |
delims.insert(make_pair(')', '(')); | |
delims.insert(make_pair(']', '[')); | |
delims.insert(make_pair('}', '{')); | |
for (; *curChar != 0; curChar++) if (*curChar == '(' || *curChar == '[' || | |
*curChar == '{') store.push(*curChar); | |
else if ((*curChar == ')' || *curChar == ']' || *curChar == '}') && | |
!store.empty() && store.top() == delims[*curChar]) store.pop(); | |
else return false; | |
return store.empty(); | |
} | |
int main() { | |
std::string delimStr; | |
std::getline(std::cin, delimStr); | |
std::cout << (IsBalanced(delimStr.c_str()) ? "True" : "False"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment