-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
58 lines (50 loc) · 1.36 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4_kyu__All_Balanced_Parentheses
{
class Program
{
static void Main(string[] args)
{
var warriorsList = new List<string>();
warriorsList = Balanced.BalancedParens(4);
warriorsList.Sort();
}
}
public class Balanced
{
public static List<string> BalancedParens(int n)
{
List<string> r = new List<string>();
int left_count = 0;
int right_count = 0;
if (n == 0)
{
r.Add(string.Empty);
return r;
}
string s = "(";
left_count = 1;
JoinString(n, left_count, right_count, s, r);
return r;
}
public static void JoinString(int n, int left_count, int right_count, string s, List<string> r)
{
if (left_count == right_count && right_count == n)
{
r.Add(s);
return;
}
if (left_count < n)
{
JoinString(n, left_count + 1, right_count, s + "(", r);
}
if (right_count < left_count)
{
JoinString(n, left_count, right_count + 1, s + ")", r);
}
}
}
}