Valid Parentheses - String - Easy - LeetCode
💻 coding

Valid Parentheses - String - Easy - LeetCode

2 min read 304 words
2 min read
ShareWhatsAppPost on X
  • 1A string of parentheses is valid if all open brackets are closed by the same type and in the correct order.
  • 2Examples demonstrate valid strings like '()' and '{[]}', while '(]' and '([)]' are invalid.
  • 3The solution uses a stack to track open brackets, ensuring efficient validation with O(n) time and space complexity.

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"A string of parentheses is valid if all open brackets are closed by the same type and in the correct order."

Valid Parentheses - String - Easy - LeetCode

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets.

Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"

Output: true

Example 2:

Input: "()[]{}"

Output: true

Example 3:

Input: "(]"

Output: false

Example 4:

Input: "([)]"

Output: false

Example 5:

Input: "{[]}"

Output: true

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.String
{
 public class IsValidSoln
 {
 public bool IsValid(string s)
 {
 var stack = new Stack<char>();
 var map = new Dictionary<char, char>();
 map.Add('(', ')');
 map.Add('{', '}');
 map.Add('[', ']');

 for (int i = 0; i < s.Length; i++)
 {
 if (map.ContainsKey(s[i]))
 {
 stack.Push(s[i]);
 }
 else
 {
 if(stack.Count == 0)
 {
 return false;
 }

 var ch = stack.Pop();
 if(map[ch] != s[i])
 {
 return false;
 }
 }
 }

 if(stack.Count > 0)
 {
 return false;
 }

 return true;
 }
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Unit Tests:

using LeetCode.AskGif.Easy.String;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.String
{
 [TestClass]
 public class IsValidSolnTests
 {
 [TestMethod]
 public void IsValidSoln_First()
 {
 var input = "()";
 var output = true;
 var res = new IsValidSoln().IsValid(input);

 Assert.AreEqual(res, output);
 }

 [TestMethod]
 public void IsValidSoln_Second()
 {
 var input = "()[]{}";
 var output = true;
 var res = new IsValidSoln().IsValid(input);

 Assert.AreEqual(res, output);
 }

 [TestMethod]
 public void IsValidSoln_Third()
 {
 var input = "(]";
 var output = false;
 var res = new IsValidSoln().IsValid(input);

 Assert.AreEqual(res, output);
 }

 [TestMethod]
 public void IsValidSoln_Fourth()
 {
 var input = "([)]";
 var output = false;
 var res = new IsValidSoln().IsValid(input);

 Assert.AreEqual(res, output);
 }

 [TestMethod]
 public void IsValidSoln_Fifth()
 {
 var input = "{[]}";
 var output = true;
 var res = new IsValidSoln().IsValid(input);

 Assert.AreEqual(res, output);
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 4 June 2020 · 2 min read · 304 words

Part of AskGif Blog · coding

You might also like