mirror of
https://github.com/Manoj-HV30/dsa-competitive-programming.git
synced 2026-05-16 19:35:22 +00:00
leetcode new problems
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
class Solution{
|
||||
public:
|
||||
bool isValid(string s){
|
||||
stack<char> st;
|
||||
for(char c : s){
|
||||
if(c=='(' || c == '{' || c == '[') st.push(c);
|
||||
else{
|
||||
if(st.empty()) return false;
|
||||
|
||||
char top = st.top();
|
||||
st.pop();
|
||||
|
||||
if((c==')' && top!='(') || (c == '}' && top!= '{') || (c == ']' && top!= '[')) return false;
|
||||
}
|
||||
}
|
||||
return st.empty();
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
class Solution {
|
||||
public:
|
||||
bool isValid(string s) {
|
||||
stack<char> st;
|
||||
unordered_map<char,char> mapping = {{')','('}, {'}','{'}, {']','['}};
|
||||
for(char c: s){
|
||||
if(mapping.find(c) == mapping.end()) st.push(c);
|
||||
else if(!st.empty() && mapping[c] == st.top()) st.pop();
|
||||
else return false;
|
||||
|
||||
}
|
||||
return st.empty();
|
||||
|
||||
|
||||
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user