Let's see what you guys think...
#include <iostream> #include <stack> #include <string> bool isValidCurlyBraceExpression(std::string expressionToCheck); void test(std::string expression, bool expectation); int main(int argc, char* argv[]) { test("{{{}{}}}", true); test("{{", false); test("{}}", false); test("}{", false); test("", true); return 0; } void test(std::string expression, bool expectation){ bool returnValue = isValidCurlyBraceExpression(expression); if(returnValue == expectation) std::cout << "Test succeeded \"" << expression << "\" = " << returnValue << std::endl; else std::cerr << "Test failed! \"" << expression << "\" = " << returnValue << std::endl; } bool isValidCurlyBraceExpression(std::string expressionToCheck) { std::stack<char> openedBraces; for (auto it = expressionToCheck.begin(); it != expressionToCheck.end(); ++it) { if (*it == '{') { openedBraces.push('{'); } else if (*it == '}') { if (openedBraces.empty()) return false; openedBraces.pop(); } } if (openedBraces.empty()) return true; else return false; }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Let's see what you guys think...
C++