# 20. Valid Parentheses
Difficulty: Easy
## Solution
```cpp=
/**
*** Author: R-CO
*** E-mail: daniel1820kobe@gmail.com
*** Date: 2020-10-30
**/
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
#include <stack>
using std::stack;
#include <string>
using std::string;
#include <vector>
using std::vector;
struct TestCasesStruct {
string input;
bool expected_output;
};
class Solution {
public:
static bool isValid(string s) {
stack<char> char_stack;
for (auto c : s) {
switch (c) {
case '(':
case '[':
case '{': {
char_stack.push(c);
} break;
case ')': {
if (!operateWithStack('(', char_stack)) {
return false;
}
} break;
case ']': {
if (!operateWithStack('[', char_stack)) {
return false;
}
} break;
case '}': {
if (!operateWithStack('{', char_stack)) {
return false;
}
} break;
}
}
return (char_stack.empty()) ? true : false;
}
private:
inline static bool operateWithStack(const char left_parenthese,
stack<char> &char_stack) {
if (char_stack.empty() || char_stack.top() != left_parenthese) {
return false;
}
char_stack.pop();
return true;
}
};
int main(int argc, char *argv[]) {
vector<TestCasesStruct> test_cases = {{"()", true},
{"()[]{}", true},
{"(]", false},
{"([)]", false},
{"}", false}};
size_t case_index = 0;
for (auto &test_case : test_cases) {
if (Solution::isValid(test_case.input) == test_case.expected_output) {
cout << "Case [" << case_index << "] is pass" << endl;
} else {
cout << "Case [" << case_index << "] is fail" << endl;
}
++case_index;
}
return EXIT_SUCCESS;
}
```
## Result
Success
Details
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Valid Parentheses.
Memory Usage: 6.7 MB, less than 35.41% of C++ online submissions for Valid Parentheses.
###### tags: `LeetCode-Easy` `C++`