Given a string that store some characters
Approach
1. from begin we have created a string as st
2. and the next step we have to create a map that stores the character
3. and store the value in the map
4. in this step we have initialized the value of st
5. after that we have traversed the entire string
6. in this step we checked the condition and after that remove the character.
7. in the last step we have to return
Code
class Solution {
public:
bool isValid(string s) {
string st;
map<char, char>m;
m['(']=')';
m['{']='}';
m['[']=']';
st.push_back(s[0]);
for(int i=1; i<s.size(); i++){
if(m[st.back()]==s[i]){
st.pop_back();
}
else
st.push_back(s[i]);
}
if(st.size()!=0){
return 0;
}
return 1;
}
};
0 Comments