-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_pair_of_bracket.h
More file actions
46 lines (40 loc) · 1.09 KB
/
Copy pathcheck_pair_of_bracket.h
File metadata and controls
46 lines (40 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
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
#ifndef CPP_ALGORITHM_CHECK_PAIR_OF_BRACKET_H
#define CPP_ALGORITHM_CHECK_PAIR_OF_BRACKET_H
#include <stack>
#include <string>
#include <unordered_map>
namespace PairOfBracket
{
/**
* \brief Checks if the input string contains bracket pairs and is well-formed.
* \param input The input string.
* \return True if the input string contains a pair of bracket, false otherwise.
*/
bool CheckPairOfBracket(const std::string& input);
}
// ----------------------------------------------------------------------------
inline bool PairOfBracket::CheckPairOfBracket(const std::string& input)
{
std::stack<char> stack;
const std::unordered_map<char, char> bracket_pairs = {
{')', '('},
{']', '['},
{'}', '{'}};
for (char ch : input)
{
if (!bracket_pairs.contains(ch))
{
stack.push(ch);
}
else
{
if (stack.empty() || stack.top() != bracket_pairs.at(ch))
{
return false;
}
stack.pop();
}
}
return stack.empty();
}
#endif