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 9 minutes
Notes
not defined yet
Code: 07:03:33 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private static readonly char[] openingBrackets = new char[] {'(', '{', '['};
private static readonly char[] closingBrackets = new char[] {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count != 0;
}
}
Analysis
Compile error
Compilation failed: 8 error(s), 0 warnings Solution.cs(13,9): error CS0246: The type or namespace name `Stack' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(15,33): 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? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(16,17): error CS0841: A local variable `brackets' cannot be used before it is declared Solution.cs(19,33): 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? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(20,21): error CS0841: A local variable `brackets' cannot be used before it is declared Solution.cs(24,45): error CS0841: A local variable `brackets' cannot be used before it is declared Solution.cs(24,37): error CS1501: No overload for method `IndexOf' takes `1' arguments /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(30,16): error CS0841: A local variable `brackets' cannot be used before it is declared
Code: 07:03:47 UTC,
cs,
verify,
result: Failed
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly char[] openingBrackets = new char[] {'(', '{', '['};
private readonly char[] closingBrackets = new char[] {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count != 0;
}
}
Analysis
Compile error
Compilation failed: 8 error(s), 0 warnings Solution.cs(13,9): error CS0246: The type or namespace name `Stack' could not be found. Are you missing `System.Collections.Generic' using directive? Solution.cs(15,33): 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? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(16,17): error CS0841: A local variable `brackets' cannot be used before it is declared Solution.cs(19,33): 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? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(20,21): error CS0841: A local variable `brackets' cannot be used before it is declared Solution.cs(24,45): error CS0841: A local variable `brackets' cannot be used before it is declared Solution.cs(24,37): error CS1501: No overload for method `IndexOf' takes `1' arguments /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(30,16): error CS0841: A local variable `brackets' cannot be used before it is declared
Code: 07:04:06 UTC,
cs,
verify,
result: Failed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly char[] openingBrackets = new char[] {'(', '{', '['};
private readonly char[] closingBrackets = new char[] {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count != 0;
}
}
Analysis
Compile error
Compilation failed: 4 error(s), 0 warnings Solution.cs(16,33): 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? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(20,33): 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? /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(25,37): error CS1501: No overload for method `IndexOf' takes `1' arguments /opt/lang/mono/lib/mono/4.5/mscorlib.dll (Location of the symbol related to previous error) Solution.cs(31,25): error CS0029: Cannot implicitly convert type `bool' to `int'
Code: 07:05:03 UTC,
cs,
verify,
result: Failed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count != 0;
}
}
Analysis
Compile error
Compilation failed: 1 error(s), 0 warnings Solution.cs(31,25): error CS0029: Cannot implicitly convert type `bool' to `int'
Code: 07:05:13 UTC,
cs,
verify,
result: Failed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count != 0 ? 1 : 0;
}
}
Analysis
Code: 07:05:46 UTC,
cs,
verify,
result: Failed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
Console.WriteLine("Closing before opening");
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
Console.WriteLine("Mismatch");
return 0;
}
}
}
return brackets.Count != 0 ? 1 : 0;
}
}
Analysis
Code: 07:06:00 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
Console.WriteLine("Closing before opening");
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
Console.WriteLine("Mismatch");
return 0;
}
}
}
return brackets.Count == 0 ? 1 : 0;
}
}
Analysis
Code: 07:06:05 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count == 0 ? 1 : 0;
}
}
Analysis
Code: 07:06:10 UTC,
cs,
verify,
result: Passed
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count == 0 ? 1 : 0;
}
}
Analysis
Code: 07:06:12 UTC,
cs,
final,
score: 
100
using System;
using System.Collections.Generic;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
private readonly List<char> openingBrackets = new List<char> {'(', '{', '['};
private readonly List<char> closingBrackets = new List<char> {')', '}', ']'};
public int solution(string S) {
Stack<char> brackets = new Stack<char>();
foreach (char c in S) {
if (openingBrackets.Contains(c)) {
brackets.Push(c);
}
if (closingBrackets.Contains(c)) {
if (brackets.Count == 0) {
return 0;
}
if (openingBrackets.IndexOf(brackets.Pop()) != closingBrackets.IndexOf(c)) {
return 0;
}
}
}
return brackets.Count == 0 ? 1 : 0;
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N)
expand all
Correctness tests
1.
0.064 s
OK
2.
0.065 s
OK
3.
0.065 s
OK
4.
0.068 s
OK
5.
0.064 s
OK
1.
0.060 s
OK
1.
0.065 s
OK
2.
0.065 s
OK
3.
0.064 s
OK
4.
0.064 s
OK
5.
0.067 s
OK
expand all
Performance tests
1.
0.090 s
OK
2.
0.066 s
OK
3.
0.066 s
OK
1.
0.068 s
OK
2.
0.064 s
OK
3.
0.065 s
OK
1.
0.088 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.071 s
OK
2.
0.072 s
OK
3.
0.074 s
OK
4.
0.075 s
OK
5.
0.068 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.079 s
OK
2.
0.082 s
OK