- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <iterator>
#include <iomanip>
using namespace std;
vector<string> bracesExpressionExamples = {
"({[{}]{}[]})",
"({}}{[{}]{}[]})",
"({[{}]{}[]}",
"({[{}]{}]})",
"({[{}{}[]})",
"",
"{}"
};
string openBrace = "({[";
string closeBrace = ")}]";
typedef map<char, char> otc;
const otc& openToCloseBrace(){
static const otc o2c([](){
otc o2c;
transform(
openBrace.begin(), openBrace.end(),
closeBrace.begin(),
inserter(o2c, o2c.begin()),
[](const char open, const char close){return make_pair(open, close);}
);
return o2c;
}());
return o2c;
}
bool checkBraces (const string& e){
vector<char> s;
for(const char b: e)
if(string::npos!=openBrace.find(b))
s.push_back(openToCloseBrace().at(b));
else if(string::npos!=closeBrace.find(b) && (!s.empty()) && b==s.back())
s.pop_back();
else return false;
return s.empty();
}
int main() {
cout<<boolalpha;
transform(
bracesExpressionExamples.begin(),
bracesExpressionExamples.end(),
ostream_iterator<bool>(cout, "\n"),
checkBraces);
return 0;
}