uraharadono

Balanced Braces Test

Nov 21st, 2018
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. public class Solution
  2.     {
  3.         private static readonly Dictionary<char, char> pairs = new Dictionary<char, char>
  4.         {
  5.             { '{', '}' },
  6.             { '[', ']' },
  7.             { '(', ')' }
  8.         };
  9.  
  10.         private static char getMatchingClosingBracket(char bracket)
  11.         {
  12.             return pairs.First(s => s.Key == bracket).Value;
  13.         }
  14.  
  15.         private static bool isOpeningBracket(char bracket)
  16.         {
  17.             return pairs.ContainsKey(bracket);
  18.         }
  19.  
  20.         private static bool isBracket(char input)
  21.         {
  22.             return (pairs.ContainsKey(input) || pairs.ContainsValue(input));
  23.         }
  24.  
  25.         private static bool isBalanced(string inputString)
  26.         {
  27.             #region Not used in turned in solution
  28.             // NOT COMMITTED: before everything check if input is proper meaning if we have odd number of elements, answer is "NO" always
  29.             // I could not commit this, because they added random "chars" in their examples so I had sometimes odd array sizes e.g. "[ab]c{(d5) (11)}" or "a[b]c)d"
  30.  
  31.             // before everything check if input is proper, meaning if we have odd number of elements, answer is always "false"
  32.             //if (inputString.Length % 2 != 0)
  33.             //    return false;
  34.             #endregion
  35.  
  36.  
  37.             // Using stack as opposed to list because "pop" function, list doesn't have "POP" function as seen here: https://stackoverflow.com/a/24855955
  38.             // (could implement my own, but I don't know how will this in-browser compiler react to it)
  39.             Stack<char> containList = new Stack<char>();
  40.  
  41.             // Loop trough array
  42.             for (int i = 0; i < inputString.Length; i++)
  43.             {
  44.                 // Check if this is opening bracket
  45.                 if (isOpeningBracket(inputString[i]))
  46.                 {
  47.                     // If it is, push it in our stack
  48.                     containList.Push(inputString[i]);
  49.                 }
  50.                 else // If not
  51.                 {
  52.                     // Check if given string is a bracket at all
  53.                     if (!isBracket(inputString[i]))
  54.                     {
  55.                         // If it isn't just continue
  56.                         continue;
  57.                     }
  58.  
  59.                     // If there are only closing brackets in string
  60.                     if (containList.Count == 0 && inputString.Length - 1 == i)
  61.                         return false;
  62.  
  63.                     // If there is nothing in our stack, continue spinning
  64.                     if (containList.Count == 0)
  65.                         continue;
  66.  
  67.                     // Check if we have closing bracket for this input
  68.                     if (getMatchingClosingBracket(containList.First()) == (inputString[i]))
  69.                     {
  70.                         // Once we find our closing bracket, pop it from stack (Note: Stack is FIFO)
  71.                         containList.Pop();
  72.                     }
  73.                     else
  74.                     {
  75.                         // Return result immediately, because something went horribly wrong :O
  76.                         return false;
  77.                     }
  78.                 }
  79.             }
  80.  
  81.             return containList.Count <= 0;
  82.         }
  83.     }
Advertisement
Add Comment
Please, Sign In to add comment