Example #1
0
        public static string GetParenthesis(int n)
        {
            var result = new List <string>();

            if (n < 1)
            {
                return(null);
            }

            Queue <ParenthesisItem> queue = new Queue <ParenthesisItem>();
            var currentItem = new ParenthesisItem();

            currentItem.AddOpen(currentItem);
            queue.Enqueue(currentItem);
            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                if (current.Count == 2 * n)
                {
                    result.Add(current.Output);
                    continue;
                }

                if (current.OpenCount < n)
                {
                    var tempItem = new ParenthesisItem();
                    tempItem.AddOpen(current);
                    queue.Enqueue(tempItem);
                }
                if (current.CloseCount < current.OpenCount)
                {
                    var tempItem = new ParenthesisItem();
                    tempItem.AddClose(current);
                    queue.Enqueue(tempItem);
                }
            }
            var sb = new StringBuilder();

            result.ForEach(res => sb.Append(res + ", "));
            return(sb.Length > 2 ? sb.ToString().Substring(0, sb.Length - 2) : sb.ToString());
        }
Example #2
0
 public void AddClose(ParenthesisItem item)
 {
     sb        = new StringBuilder(item.Output);
     OpenCount = item.OpenCount;
     sb.Append(')');
 }
Example #3
0
 public void AddOpen(ParenthesisItem item)
 {
     sb = new StringBuilder(item.Output);
     sb.Append('(');
     OpenCount = item.OpenCount + 1;
 }