bool isBalanced(const std::string& s) { int count = 0; for (size_t i = 0; i < s.length(); ++i) { if (s[i] == '(') { ++count; } else if (s[i] == ')') { if (--count < 0) { return false; } } } return true; }
- bool isBalanced(const std::string& s) {
- int count = 0;
for (size_t i = 0; i < s.length(); i++) {- for (size_t i = 0; i < s.length(); ++i) {
- if (s[i] == '(') {
count++;- ++count;
- }
if (s[i] == ')') {if (count <= 0) { return false; }count--;- else if (s[i] == ')') {
- if (--count < 0) { return false; }
- }
- }
- return true;
- }
Fundamentals
Strings
std::string digest(const std::string& param) { std::string result; result.reserve(param.size() * 2); // Ensure exactly one memory allocation for (char letter: param) { result.push_back(letter); result.push_back(' '); } result.pop_back(); return result; }
std::string digest(const std::string param) {- std::string digest(const std::string& param) {
- std::string result;
- result.reserve(param.size() * 2); // Ensure exactly one memory allocation
- for (char letter: param) {
result += std::basic_string(1, letter) + " ";- result.push_back(letter);
- result.push_back(' ');
- }
- result.pop_back();
- return result;
- }