You are viewing a single comment's thread. Return to all comments →
C++:
string isBalanced(string s) { std::map m {{'(',')'}, {'{','}'}, {'[',']'}}; std::stack st;
if (s.length() %2 != 0) return("NO"); for (int i=0;i<s.length();i++) { auto it = m.find(s[i]); if(it != m.end()) { st.push(s[i]); } else { if(st.empty()) { return("NO"); } if(m[st.top()] == s[i]) st.pop(); else return("NO"); } } if(st.empty()) return("YES"); else return("NO");
}
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
C++:
string isBalanced(string s) { std::map m {{'(',')'}, {'{','}'}, {'[',']'}}; std::stack st;
}