-
[HackerRank] Balanced Brackets c++Coding Test/HackerRank 2023. 5. 15. 22:13728x90
- Problem
Balanced Brackets | HackerRank
Given a string containing three types of brackets, determine if it is balanced.
www.hackerrank.com
- Solutions
stack을 사용하는 대표적인 문제 유형이다.
/* * Complete the 'isBalanced' function below. * * The function is expected to return a STRING. * The function accepts STRING s as parameter. */ string isBalanced(string s) { string result(""); stack<char> st; map<char, char> hash; hash['('] = ')'; hash['{'] = '}'; hash['['] = ']'; for(const auto& c : s){ if('(' == c || '{' == c || '[' == c ){ st.push(c); } else{ if(st.empty()){ return "NO"; } if(hash[st.top()] == c){ st.pop(); } else{ return "NO"; } } } result = st.empty() ? "YES" : "NO"; return result; }
728x90'Coding Test > HackerRank' 카테고리의 다른 글
[HackerRank] Simple Text Editor c++ (0) 2023.05.17 [HackerRank] Tower Breakers c++ (0) 2022.07.20 [HackerRank] Zig Zag Sequence C++ (0) 2022.07.20 [HackerRank] counting sort 1 c++ (0) 2022.07.20 [HackerRank] Lonely Integer c++ (0) 2022.07.20