# This blog is about stack in LeetCode I wrote

Question 1 (corresponding to question 20 of LeetCode question bank) (simple question!)

(of course, if you don't understand your own summary, you can go to the website and read the corresponding topic to see the solution!)

Title: valid parentheses (parenthesis matching problem of string)

(I wrote a hundred lines at the first time, but I couldn't deal with all the matching problems of bracket strings. Finally, I learned from the answers! I have to brush this question 2-10!)

Given one, only '(', '' ',' {','} ',' [','] '   String s to determine whether the string is valid.

Valid strings must meet:

1. The left parenthesis must be closed with the same type of right parenthesis.
2. The left parentheses must be closed in the correct order.

Returns true if there are links in the linked list. Otherwise, false is returned.

Here is an example: Solution 1:

Idea: because we want to match the left parenthesis after the parenthesis character element in the string containing only parentheses to the corresponding right parenthesis. Therefore, to judge the validity of parentheses, we can use the data structure of "stack".

We iterate over the given string s. When we encounter a left parenthesis, we expect a right parenthesis of the same type to close it in subsequent iterations. Since the left parenthesis encountered later must be closed first, we can put the left parenthesis directly into the top of the stack in order.

When we encounter a closing bracket, we need to close it with an opening bracket of the same type. At this point, we can take out the left parentheses at the top of the stack and judge whether they are the same type of parentheses. If it is not of the same type, or if there is no left parenthesis in the stack, the string s   Invalid, return False. To quickly determine the type of parentheses, we can use the hash table unordered_map to store each parenthesis. The Key key of the hash table is the right parenthesis, and the Value is the left parenthesis of the same type.

After the traversal, if there is no left parenthesis in the stack, it means that we will use the string s   Returns True if all open parentheses in are closed, otherwise False.

Note that the length of the valid string must be even, so if the length of the string is odd, we can directly return False, eliminating the subsequent traversal judgment process.

See the correct code below:

```class Solution {
public:
bool isValid(string s) {
int n = s.size();
if (n % 2 == 1) {
return false;
}//if the total number of parentheses is odd, it must not be a string matching parentheses!

unordered_map<char, char> pairs = {
//{key,value},
{')', '('},
{']', '['},
{'}', '{'}
};
stack<char> stk;
for (char ch : s) {
if (pairs.count(ch)) {//pairs.count(ch) this code indicates that ch exists in the specified parentheses!
//Here through unordered_map[key] == value to find value!
if (stk.empty() || stk.top() != pairs[ch]) {
return false;
}
stk.pop();
}
else {
//That is to ignore the meaning of the left bracket!
//if it is an open parenthesis, just push it into the stack!
stk.push(ch);
}
}
return stk.empty();
}
};```

I later wrote codes based on the answers:

```class Solution {
public:
bool isValid(string s) {
//First, if original parenthesis string is a string with the an odd number of the parentheses
//Then it must be a non bracket matching string!
int len = s.size();
if (len % 2 == 1) {
return false;
}
unordered_map<char, char> pairs;
pairs.insert(make_pair(')', '('));
pairs.insert(make_pair(']', '['));
pairs.insert(make_pair('}', '{'));
stack<char> stk;
for (char ch : s) {
if (pairs.count(ch)) {
if (stk.empty() || stk.top() != pairs[ch]) {
return false;
}
stk.pop();
}
else {
stk.push(ch);//Ignore the left parenthesis! That is, put the left bracket on the stack!
}
}
return stk.empty();
}
};```

It can be noted that here I write:

unordered_map<char, char> pairs;
pairs.insert(make_pair(')', '('));
pairs.insert(make_pair(']', '['));
pairs.insert(make_pair('}', '{'));

unordered_map<char, char> pairs = {
//{key,value},
{')', '('},
{']', '['},
{'}', '{'}
};

I think both methods of creating hash tables are OK. Of course, the solution of LeetCode standard directly creates hash tables like creating arrays, which is easier to understand! I can learn!

(

For example, the creation method of this array is used in the hash table, which is worth learning!:

int a[]= {

{0,1,2},

{2,3,4},

{5,6,7},

};)

Posted by jeroom on Sat, 02 Oct 2021 13:12:27 -0700