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:
class Solution { public 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–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used C#
Time spent on task 4 minutes
Notes
not defined yet
Code: 12:14:50 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(string S) {
int N = S.Length;
if(N == 0)
return 1;
var matched = new Dictionary<char, char> {{']','['}, {'}','{'}, {')', '('}};
var to_push = new char[] {'[', '{', '(' };
var stack = new List<char>();
for(int index = 0; index < N; index++)
{
var el = S[index];
if(to_push.Contains(el))
{
stack.Add(el);
}
else
{
int stackCount = stack.Count() - 1;
if (stackCount == -1)
return 0;
else if ( matched[el] != stack[stackCount])
return 0;
else
stack.RemoveAt(stackCount);
}
}
return stack.Count() == 0 ? 1 : 0;
}
}
Analysis
Compile error
Compilation failed: 3 error(s), 0 warnings Solution.cs(13,27): error CS0246: The type or namespace name `Dictionary' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(15,25): error CS0246: The type or namespace name `List' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(20,24): error CS1061: Type `char[]' does not contain a definition for `Contains' and no extension method `Contains' of type `char[]' could be found. Are you missing `System.Linq' using directive? /usr/share/mono-codility/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error)
Code: 12:15:04 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(string S) {
int N = S.Length;
if(N == 0)
return 1;
var matched = new Dictionary<char, char> {{']','['}, {'}','{'}, {')', '('}};
var to_push = new char[] {'[', '{', '(' };
var stack = new List<char>();
for(int index = 0; index < N; index++)
{
var el = S[index];
if(to_push.Contains(el))
{
stack.Add(el);
}
else
{
int stackCount = stack.Count() - 1;
if (stackCount == -1)
return 0;
else if ( matched[el] != stack[stackCount])
return 0;
else
stack.RemoveAt(stackCount);
}
}
return stack.Count() == 0 ? 1 : 0;
}
}
Analysis
Compile error
Compilation failed: 3 error(s), 0 warnings Solution.cs(20,24): error CS1061: Type `char[]' does not contain a definition for `Contains' and no extension method `Contains' of type `char[]' could be found. Are you missing `System.Linq' using directive? /usr/share/mono-codility/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(26,40): error CS1955: The member `System.Collections.Generic.List<char>.Count' cannot be used as method or delegate Solution.cs(38,22): error CS1955: The member `System.Collections.Generic.List<char>.Count' cannot be used as method or delegate
Code: 12:15:22 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
using System.Linq;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(string S) {
int N = S.Length;
if(N == 0)
return 1;
var matched = new Dictionary<char, char> {{']','['}, {'}','{'}, {')', '('}};
var to_push = new char[] {'[', '{', '(' };
var stack = new List<char>();
for(int index = 0; index < N; index++)
{
var el = S[index];
if(to_push.Contains(el))
{
stack.Add(el);
}
else
{
int stackCount = stack.Count() - 1;
if (stackCount == -1)
return 0;
else if ( matched[el] != stack[stackCount])
return 0;
else
stack.RemoveAt(stackCount);
}
}
return stack.Count() == 0 ? 1 : 0;
}
}
Analysis
Code: 12:15:28 UTC,
cs,
verify,
result: Passed
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
using System.Linq;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(string S) {
int N = S.Length;
if(N == 0)
return 1;
var matched = new Dictionary<char, char> {{']','['}, {'}','{'}, {')', '('}};
var to_push = new char[] {'[', '{', '(' };
var stack = new List<char>();
for(int index = 0; index < N; index++)
{
var el = S[index];
if(to_push.Contains(el))
{
stack.Add(el);
}
else
{
int stackCount = stack.Count() - 1;
if (stackCount == -1)
return 0;
else if ( matched[el] != stack[stackCount])
return 0;
else
stack.RemoveAt(stackCount);
}
}
return stack.Count() == 0 ? 1 : 0;
}
}
Analysis
Code: 12:15:31 UTC,
cs,
final,
score: 
100
using System;
// you can also use other imports, for example:
using System.Collections.Generic;
using System.Linq;
// you can use Console.WriteLine for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(string S) {
int N = S.Length;
if(N == 0)
return 1;
var matched = new Dictionary<char, char> {{']','['}, {'}','{'}, {')', '('}};
var to_push = new char[] {'[', '{', '(' };
var stack = new List<char>();
for(int index = 0; index < N; index++)
{
var el = S[index];
if(to_push.Contains(el))
{
stack.Add(el);
}
else
{
int stackCount = stack.Count() - 1;
if (stackCount == -1)
return 0;
else if ( matched[el] != stack[stackCount])
return 0;
else
stack.RemoveAt(stackCount);
}
}
return stack.Count() == 0 ? 1 : 0;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Performance tests
1.
0.064 s
OK
1.
0.056 s
OK
1.
0.092 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.060 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.080 s
OK