Skip to content

Instantly share code, notes, and snippets.

@MSK61
Last active August 29, 2015 14:02
Show Gist options
  • Save MSK61/e7256e3a0eab5d723204 to your computer and use it in GitHub Desktop.
Save MSK61/e7256e3a0eab5d723204 to your computer and use it in GitHub Desktop.
#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