-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution1.cpp
35 lines (35 loc) · 887 Bytes
/
Solution1.cpp
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
// Problem: https://leetcode.com/problems/merge-two-sorted-lists/
// Author: github.com/ankuralld5999
// Time: O(n)
// Space: O(n)
class Solution {
public:
bool isValid(string s) {
int n = s.size();
if(n%2!=0)
return false;
stack<char> st;
for(auto x : s){
if(x==')'){
if(st.empty() || st.top()!='(')
return false;
st.pop();
}
else if(x=='}'){
if(st.empty() || st.top()!='{')
return false;
st.pop();
}
else if(x==']'){
if(st.empty() || st.top()!='[')
return false;
st.pop();
}
else
st.push(x);
}
if(st.empty())
return true;
return false;
}
};