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 5 minutes
Notes
not defined yet
Task timeline
Code: 12:29:33 UTC,
java,
autosave
Code: 12:29:44 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:29:48 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:30:40 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
cout << hi
if(st.empty()) return 1;
else return 0;
}
Code: 12:30:46 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
cout << "hi" << endl;
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:31:01 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:31:20 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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;
cout << "hi"<<endl;
char c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:31:31 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
cout << "hi"<<endl;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:31:32 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
cout << "hi"<<endl;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:31:43 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || S.empty())
return 1;
cout << "hi"<<endl;
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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:31:51 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || S.empty())
return 1;
cout << "hi"<<endl;
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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:32:01 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:32:06 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() / 2 == 1)
return 0;
cout << "hi"<<endl;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:32:17 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
// if(S.size() / 2 == 1)
// return 0;
cout << "hi"<<endl;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:32:42 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
// if(S.size() / 2 == 1)
return 0;
cout << "hi"<<endl;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:33:10 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
cout << S.size() <<endl;
if(S.size() / 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:33:25 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
cout << S.size() <<endl;
if(S.size() 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:33:35 UTC,
cpp,
autosave
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() % 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
Code: 12:33:36 UTC,
cpp,
verify,
result: Passed
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() % 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
User test case 1:
['()']
Analysis
Code: 12:33:39 UTC,
cpp,
final,
score: 
100
#include <stack>
using namespace std;
int solution(string &S) {
// write your code in C++14 (g++ 6.2.0)
stack<char> st;
if(S.size() % 2 == 1)
return 0;
if( S == "" || 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 c = st.top();
st.pop();
if(c == '(' && S[i] != ')')
return 0;
else if(c == '{' && S[i] != '}')
return 0;
else if(c == '[' && S[i] != ']')
return 0;
}
}
if(st.empty()) return 1;
else return 0;
}
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