Tasks Details
easy
1.
Brackets
Determine whether a given string of parentheses (multiple types) is properly nested.
Task Score
100%
Correctness
100%
Performance
100%
A string S consisting of N characters is considered to be properly nested if any of the following conditions is true:
- S is empty;
- S has the form "(U)" or "[U]" or "{U}" where U is a properly nested string;
- S has the form "VW" where V and W are properly nested strings.
For example, the string "{[()()]}" is properly nested but "([)()]" is not.
Write a function:
int solution(string &S);
that, given a string S consisting of N characters, returns 1 if S is properly nested and 0 otherwise.
For example, given S = "{[()()]}", the function should return 1 and given S = "([)()]", the function should return 0, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..200,000];
- string S is made only of the following characters: '(', '{', '[', ']', '}' and/or ')'.
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C++
Time spent on task 14 minutes
Notes
not defined yet
Task timeline
Code: 12:58:00 UTC,
java,
autosave
Code: 12:58:35 UTC,
cpp,
autosave
Code: 12:58:56 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
return res;
}
Code: 12:59:11 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
return res;
}
Code: 12:59:28 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.length)
return res;
}
Code: 13:00:00 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<str)
return res;
}
Code: 13:00:11 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
}
return res;
}
Code: 13:00:27 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='')
}
return res;
}
Code: 13:00:40 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(')
}
return res;
}
Code: 13:01:06 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='')
}
return res;
}
Code: 13:01:36 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
res =
}
}
}
return res;
}
Code: 13:01:46 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
}
}
return res;
}
Code: 13:02:30 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
if
}
}
return res;
}
Code: 13:02:50 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
if(S[i] == ')' )
}
}
return res;
}
Code: 13:03:42 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
s
if(S[i] == ')' )
}
}
return res;
}
Code: 13:03:52 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
st.pop
if(S[i] == ')' )
}
}
return res;
}
Code: 13:04:22 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
pop_cst.pop();
if(S[i] == ')' )
}
}
return res;
}
Code: 13:04:34 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' )
}
}
return res;
}
Code: 13:05:04 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='')
}
}
return res;
}
Code: 13:05:19 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='(') return 0;
else if
}
}
return res;
}
Code: 13:05:32 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if
}
}
return res;
}
Code: 13:05:49 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == ']' && char_last !='[')
}
}
return res;
}
Code: 13:06:14 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
return res;
}
Code: 13:06:37 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}
return res;
}
Code: 13:06:46 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<strlen(S); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::__cxx11::string&)': func.cpp:14:28: error: cannot convert 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const char*' for argument '1' to 'size_t strlen(const char*)' for(int i=0; i<strlen(S); i++){ ^ func.cpp:15:41: error: lvalue required as left operand of assignment if(S[i]='(' || S[i]='[' || S[i]='{'){ ^~~ func.cpp:21:13: error: 'char_last' was not declared in this scope char_last = st.pop(); ^~~~~~~~~ func.cpp:31:13: error: missing template arguments before '.' token if(stack.isEmpty()){ ^
Code: 13:07:57 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S./si; i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:08:07 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:08:33 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
if(S.empty()){
return 1;
}
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:08:43 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
if(S.empty()){
return 1;
}
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:09:23 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:09:46 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:10:08 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:10:17 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(stack.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::__cxx11::string&)': func.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int i=0; i<S.size(); i++){ ~^~~~~~~~~ func.cpp:15:41: error: lvalue required as left operand of assignment if(S[i]='(' || S[i]='[' || S[i]='{'){ ^~~ func.cpp:33:13: error: missing template arguments before '.' token if(stack.isEmpty()){ ^
Code: 13:10:33 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]='(' || S[i]='[' || S[i]='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(st.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:10:52 UTC,
cpp,
verify,
result: Failed
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]=='(' || S[i]=='[' || S[i]=='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(st.isEmpty()){
res = 1;
}else{
res = 0;
}
return res;
}
Analysis
Compile error
func.cpp: In function 'int solution(std::__cxx11::string&)': func.cpp:14:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int i=0; i<S.size(); i++){ ~^~~~~~~~~ func.cpp:33:11: error: 'class std::stack<char>' has no member named 'isEmpty'; did you mean 'empty'? if(st.isEmpty()){ ^~~~~~~
Code: 13:11:03 UTC,
cpp,
autosave
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]=='(' || S[i]=='[' || S[i]=='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(st.empty()){
res = 1;
}else{
res = 0;
}
return res;
}
Code: 13:11:04 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]=='(' || S[i]=='[' || S[i]=='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(st.empty()){
res = 1;
}else{
res = 0;
}
return res;
}
Analysis
Code: 13:11:13 UTC,
cpp,
verify,
result: Passed
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]=='(' || S[i]=='[' || S[i]=='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(st.empty()){
res = 1;
}else{
res = 0;
}
return res;
}
Analysis
Code: 13:11:18 UTC,
cpp,
final,
score: 
100
// you can use includes, for example:
#include <bits/stdc++.h>
using namespace std;
// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
int res;
stack<char> st;
for(int i=0; i<S.size(); i++){
if(S[i]=='(' || S[i]=='[' || S[i]=='{'){
st.push(S[i]);
}else{
if(st.empty()){
return 0;
}
char char_last = st.top();
st.pop();
if(S[i] == ')' && char_last !='('){
return 0;
}else if(S[i] == '}' && char_last !='{'){
return 0;
}else if(S[i] == ']' && char_last !='['){
return 0;
}
}
}
if(st.empty()){
res = 1;
}else{
res = 0;
}
return res;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
5.
0.001 s
OK
1.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
5.
0.001 s
OK
expand all
Performance tests
1.
0.004 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
1.
0.004 s
OK
multiple_full_binary_trees
sequence of full trees of the form T=(TT), depths [1..10..1], with/without some brackets at the end, length=49K+
sequence of full trees of the form T=(TT), depths [1..10..1], with/without some brackets at the end, length=49K+
✔
OK
1.
0.001 s
OK
2.
0.001 s
OK
3.
0.001 s
OK
4.
0.001 s
OK
5.
0.001 s
OK
broad_tree_with_deep_paths
string of the form [TTT...T] of 300 T's, each T being '{{{...}}}' nested 200-fold, length=120K+
string of the form [TTT...T] of 300 T's, each T being '{{{...}}}' nested 200-fold, length=120K+
✔
OK
1.
0.004 s
OK
2.
0.004 s
OK