Created
February 13, 2017 14:20
-
-
Save munirwanis/2b0ba0a80a3e49646b4b934778610b13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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