Skip to content

Instantly share code, notes, and snippets.

@munirwanis
Created February 13, 2017 14:20
Show Gist options
  • Save munirwanis/2b0ba0a80a3e49646b4b934778610b13 to your computer and use it in GitHub Desktop.
Save munirwanis/2b0ba0a80a3e49646b4b934778610b13 to your computer and use it in GitHub Desktop.
using System.Linq;
using System.Collections.Generic;
public static class Groups
{
// "()" "{}" or "[]"
private static Dictionary<char, char> matches = new Dictionary<char, char> {
{ '(', ')' },
{ '{', '}' },
{ '[', ']' },
};
public static bool Check(string input)
{
if (input.Length % 2 != 0) { return false; }
string firstString = input.Substring(0, (input.Length / 2));
string lastString = input.Substring(input.Length / 2, input.Length / 2);
var firstArray = firstString.ToCharArray();
var lastArray = lastString.Reverse().ToArray();
for (int i = 0; i < firstArray.Length; i++)
{
if (matches[firstArray[i]] != lastArray[i])
{
return false;
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment