Created
December 11, 2019 22:01
-
-
Save ahmagdy/c9a7f4aaadc2addd935fdf25e3ddec2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Console.WriteLine(Convert.ToString(1000)); | |
Console.WriteLine(solution(")(")); | |
Console.WriteLine(solution("{[()()]}")); | |
Console.WriteLine(solution("([)()]")); | |
} | |
public static int solution(string S) | |
{ | |
if (string.IsNullOrEmpty(S)) | |
return 1; | |
var dicConst = new Dictionary<string, string> | |
{ | |
{"{", "}"}, | |
{"(", ")"}, | |
{"[", "]"}, | |
}; | |
var final = new Stack<string>(); | |
foreach (var chCh in S) | |
{ | |
string ch = char.ToString(chCh); | |
var key = dicConst.FirstOrDefault(d => d.Value == ch).Key; | |
if(!string.IsNullOrEmpty(key) && final.Count>0 &&final.Peek() == key) | |
final.Pop(); | |
else | |
final.Push(ch); | |
} | |
return final.Count > 0? 0 : 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment