Skip to content

Commit 483be8b

Browse files
committed
balanced parentheses
1 parent 63c7ea3 commit 483be8b

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

Advanced.Algorithms.Tests/Advanced.Algorithms.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<Compile Include="DataStructures\Tree\BinaryTree_Tests.cs" />
153153
<Compile Include="DynamicProgramming\Count\DigitCounter_Tests.cs" />
154154
<Compile Include="Geometry\PointRotation_Tests.cs" />
155+
<Compile Include="Miscellaneous\BalanceParentheses_Tests.cs" />
155156
<Compile Include="Miscellaneous\CountInversions_Tests.cs" />
156157
<Compile Include="Miscellaneous\MatrixMultiplication_Tests.cs" />
157158
<Compile Include="DynamicProgramming\Maximizing\LongestChain_Tests.cs" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Advanced.Algorithms.Miscellaneous;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace Advanced.Algorithms.Tests.Miscellaneous
10+
{
11+
[TestClass]
12+
public class BalanceParentheses_Tests
13+
{
14+
[TestMethod]
15+
public void BalanceParentheses_Smoke_Test()
16+
{
17+
Assert.AreEqual("ab", BalanceParentheses.Balance("ab"));
18+
Assert.AreEqual("ab", BalanceParentheses.Balance("((ab"));
19+
Assert.AreEqual("ab", BalanceParentheses.Balance("(ab"));
20+
Assert.AreEqual("ab", BalanceParentheses.Balance("ab))"));
21+
22+
Assert.AreEqual("((ab))", BalanceParentheses.Balance("((ab))"));
23+
Assert.AreEqual("(ab)", BalanceParentheses.Balance("(ab))"));
24+
Assert.AreEqual("(ab)", BalanceParentheses.Balance("((ab)"));
25+
26+
Assert.AreEqual("a(b)", BalanceParentheses.Balance("a(b)"));
27+
Assert.AreEqual("", BalanceParentheses.Balance("(((("));
28+
Assert.AreEqual("(()())", BalanceParentheses.Balance("(()())"));
29+
30+
Assert.AreEqual("ab()", BalanceParentheses.Balance(")ab(()"));
31+
Assert.AreEqual("ab()", BalanceParentheses.Balance(")))ab(()(("));
32+
}
33+
}
34+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace Advanced.Algorithms.Miscellaneous
7+
{
8+
public class BalanceParentheses
9+
{
10+
/// <summary>
11+
/// O(n) time complexity
12+
/// </summary>
13+
/// <param name="text"></param>
14+
/// <returns></returns>
15+
public static string Balance(string text)
16+
{
17+
var result = new StringBuilder();
18+
19+
for (int i = 0; i < text.Length; i++)
20+
{
21+
var progress = Balance(text, i, result);
22+
23+
if (progress == i)
24+
{
25+
continue;
26+
}
27+
28+
i = progress;
29+
}
30+
31+
return result.ToString();
32+
}
33+
34+
/// <summary>
35+
/// Recursively visit child parentheses and add balanced items to result
36+
/// </summary>
37+
/// <param name="text"></param>
38+
/// <param name="i"></param>
39+
/// <param name="result"></param>
40+
/// <returns></returns>
41+
public static int Balance(string text, int i, StringBuilder result)
42+
{
43+
if (i >= text.Length)
44+
{
45+
return text.Length;
46+
}
47+
48+
//close parentheses
49+
if (text[i] == ')')
50+
{
51+
return i;
52+
}
53+
54+
//a character
55+
if (text[i] != '(' && text[i] != ')')
56+
{
57+
result.Append(text[i]);
58+
return Balance(text, i + 1, result);
59+
}
60+
61+
//open parenthesis
62+
63+
var subResult = new StringBuilder();
64+
while (i + 1 < text.Length && text[i] == '(')
65+
{
66+
i = Balance(text, i + 1, subResult);
67+
68+
if (i < text.Length && text[i] == ')')
69+
{
70+
result.Append("(" + subResult.ToString() + ")");
71+
}
72+
else
73+
{
74+
result.Append(subResult.ToString());
75+
}
76+
77+
subResult.Clear();
78+
i++;
79+
}
80+
81+
return i;
82+
}
83+
}
84+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,6 @@ All are top down solutions with memoization technique.
377377

378378
- [X] Count Inversions ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/Miscellaneous/CountInversions.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/Miscellaneous/CountInversions_Tests.cs))
379379
- [X] Matrix Multiplication ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/Miscellaneous/MatrixMultiplication.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/Miscellaneous/MatrixMultiplication_Tests.cs))
380+
- [X] Balanced Parentheses ([Implementation](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms/Miscellaneous/BalanceParentheses.cs) | [Tests](https://github.com/justcoding121/Advanced-Algorithms/tree/develop/Advanced.Algorithms.Tests/Miscellaneous/BalanceParentheses_Tests.cs))
380381

382+

0 commit comments

Comments
 (0)