Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
#include <iostream>
int main(){
std::string input; getline(std::cin, input);
long unmatched(0), matched(0);
for(int k = 0; k < input.size(); k++){
if(input[k] == '('){++unmatched;}
else if(input[k] == ')' && unmatched > 0){--unmatched; ++matched;}
}
std::cout << 2 * matched << std::endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
(()))(
Output
4
Demonstration
Codeforces Solution-B. Regular Bracket Sequence-Solution in C, C++, Java, Python