Skip to content

Instantly share code, notes, and snippets.

@ahmagdy
Created December 11, 2019 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmagdy/c9a7f4aaadc2addd935fdf25e3ddec2d to your computer and use it in GitHub Desktop.
Save ahmagdy/c9a7f4aaadc2addd935fdf25e3ddec2d to your computer and use it in GitHub Desktop.
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