괄호로 된 입력값이 올바른지 판단하라.
Input: s = "()"
Output: true
Input: s = "()[]{}"
Output: true
Input: s = "(]"
Output: false
class Solution {
public:
bool isValid(string s) {
stack<char> sc;
map<char, char> m;
bool ans = true;
m.insert({ ')', '(' });
m.insert({ ']', '[' });
m.insert({ '}', '{' });
for (int i = 0; i < s.size(); i++)
{
sc.push(s[i]);
auto a = m.find(sc.top());
if (a != m.end())
{
sc.pop();
if (!sc.empty())
{
if (a->second == sc.top()) sc.pop();
else
{
ans = false;
break;
}
}
else
{
ans = false;
break;
}
}
}
if (!sc.empty()) ans = false;
return ans;
}
};
Runtime 0 ms / Memory 6.4 MB
https://leetcode.com/problems/valid-parentheses/submissions/925086095/