[LeetCode] 20. Valid Parentheses (C#)
LeetCode - Problems - Algorithms - 20. Valid Parentheses
Problem Description
Given a string s 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.
Example 1:
- Input: s = "()"
- Output: true
Example 2:
- Input: s = "()[]{}"
- Output: true
Example 3:
- Input: s = "(]"
- Output: false
Example 4:
- Input: s = "([)]"
- Output: false
Example 5:
- Input: s = "{[]}"
- Output: true
Constraints:
- 1 <= s.length <= 104
- s consists of parentheses only '()[]{}'.
์ด ๋ฌธ์ ๋ ์ฌ์๋ 12์์ LeetCode๋ฅผ ์ฒ์ ์ ํ์ ๋, ๋๋ฌด ์ด๋ ค์์ ํฌ๊ธฐํ์๋ค.
๋น์์๋ ๋ฐฐ์ฐ์ ์๋ ํด์ํ ์ด๋ธ, ์คํ ๋ฑ์ ์ด์ฉํ๋ผ๊ณ ํ๊ณ ์ด๋ฏธ์ง๋ก ๊น๋ํ๊ฒ ์ค๋ช ๋์ด์ ์ดํด๋ ๋ฌ๋๋ฐ ์ฝ๋๋ฅผ ๋ด๋ ์ดํด๊ฐ ์๋์ด์ ํฌ๊ธฐํ๊ณ ๋๊ฒผ์๋ค.
๊ทธ๋ฆฌ๊ณ ์ด ๋ฌธ์ ๋ฅผ ๋์ผ๋ก LeetCode์ ๋ฉ์ด์ก๋ค.... (์์ฐ)
์ด์ ์คํ๋ ์ธ ์ค ์๊ฒ ๋ค, ๋๋์ด ํ ์ ์๊ฒ ๊ตฐ! ํ๊ณ ๋ค์ด๊ฐ๋๋ฐ ์คํ์ ์ฌ์ฉํ์ง ์์๋ ๊ฐ๋จํ๊ฒ ํ ์ ์๋ ๋ฐฉ๋ฒ์ ์ฐพ์๋๋ค.
My Solution (C#)
public class Solution {
public bool IsValid(string s)
{
string[] validSet = { "[]", "{}", "()" };
while (s.Contains("[]") || s.Contains("{}") || s.Contains("()"))
{
s = s.Replace(validSet[0], "");
s = s.Replace(validSet[1], "");
s = s.Replace(validSet[2], "");
}
if (s == "")
{
return true;
}
else
{
return false;
}
}
}
๋๋, (์๋ ๋๋ณด๊ธฐ ํด๋ฆญ)
{
bool checker = true;
while (checker)
{
if (s.Contains("[]"))
{
s = s.Replace("[]", "");
}
else if (s.Contains("{}"))
{
s = s.Replace("{}", "");
}
else if (s.Contains("()"))
{
s = s.Replace("()", "");
}
else
{
checker = false;
}
}
if (s == "")
{
return true;
}
else
{
return false;
}
}
์์ฃผ ๊ฐ๋จํ๊ฒ Containds์ Replace ๋ฉ์๋๋ค์ ์ด์ฉํ๋ฉด ๋๋ค.
๋ค๋ง ํจ์จ์ ๊ทธ๋ฆฌ ์ข์ง ์๋ค.
- s consists of parentheses only '()[]{}'.
๋ผ๋ ๋ง์ ์ฆ, ()[]{} ์ธ์ ๊ฐ์ ํฌํจํ์ง ์์ผ๋ฉฐ, ๋ฐ๋ผ์ ์ ํจํ set๋ ๋ฌด์กฐ๊ฑด ๋ถ์ด์๊ฒ ๋๋ค.
์ฆ s ๊ฐ ์์ contains method๋ฅผ ์ด์ฉํด "()", "[]", "{}" ๊ฐ์ด ์๋์ง ํ์ธํ๊ณ ,
์กด์ฌํ๋ "()"์ "[]", "{}" ๊ฐ์ ๋ชจ๋ "" ๋น string ๊ฐ์ผ๋ก replace ๋ฐ๋ณต ํด์ฃผ๋ฉด ๋๋ค.
๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด ๋ค๋ฅธ C# ์๋ฃจ์ ์ ๋นํด ํ์ ํ ๋๋ฆฐ ์๋์์ ์ ์ ์๋ค.
Contains์ Replace method๋ ์ฃผ์ด์ง Array ํน์ String ๊ฐ์ ์ฒ์๋ถํฐ ๋๊น์ง ์ ๋ถ ํ์ด๋ณด๊ธฐ ๋๋ฌธ์ time complexity๋ O(n)์ด ๋๋ค. ๋ฐ๋ณต๋ฌธ ํ๋ฒ์ ์ด๋ฅผ O(6n) ์คํํ๋ฏ๋ก ์ต์ ์ ๊ฒฝ์ฐ O((s.Length/2) * 6 * n)์ด ๋๋ ์ ์ด๋ค.
๋ฐ๋ฉด์ Stack์ ์ด์ฉํ๊ฒ ๋๋ฉด ์ฃผ์ด์ง String ๊ฐ์ ์ฒ์๋ถํฐ ๋๊น์ง ํน์ Invalid ํ๋ค๋ฉด ์ค๊ฐ๊น์ง๋ง ์ฒดํฌํ๋ฏ๋ก ์ต์ ์ ๊ฒฝ์ฐ๊ฐ O(n)์ด๋ ํจ์ฌ ๋น ๋ฅผ ์ ๋ฐ์ ์๋ค.
๋ด์ผ์ Stack์ ์ด์ฉํด์ ํ์ด๋ด์ผ์ง.
++์ถ๊ฐ
My Solution (C#) - Using Stack
public class Solution {
public bool IsValid(string s)
{
if ( string.IsNullOrWhiteSpace(s) )
{
return false;
}
Stack<char> myStack = new Stack<char>();
Dictionary<char, char> validSet = new Dictionary<char, char>();
validSet.Add('{', '}');
validSet.Add('[', ']');
validSet.Add('(', ')');
foreach (char l in s)
{
if (validSet.ContainsKey(l)) //(validSet.ContainsKey(l))
{
myStack.Push(l);
}
else //not 0 // if (validSet.ContainsValue(l))
{
if (myStack.Count != 0 && validSet[myStack.Peek()] == l)
myStack.Pop();
else
return false;
}
}
return myStack.Count == 0 ? true : false;
}
}
์คํ์ ์ด์ฉํ ํ์ด๋ฒ.
Dictionary validSet์ ์ ์ํ๊ณ ๊ทธ ์์ opening parentheses๋ฅผ key๋ก, closing parentheses๋ฅผ value๋ก ๋ง์ถฐ ๋ฃ๋๋ค.
์ฃผ์ด์ง string s ์์ ์๋ char l์ ๋ชจ๋ ํ์ธํ๊ธฐ ์ํ foreach ๋ฐ๋ณต๋ฌธ์ ์ด์ด์ฃผ๊ณ
> ๋ง์ฝ ๊ทธ ์์ l์ด key๋ก์ ์กด์ฌํ๋ ์ง ํ์ธ ํ ๋ค, ์กด์ฌํ๋ค๋ฉด l์ myStack์ ์์์ค๋ค.
> ์๋๋ผ๋ฉด l์ ๋ฌด์กฐ๊ฑด closing parentheses์ด๋ฏ๋ก
>myStack์ด 0์ด ์๋๊ณ (&&) ๋ง์ง๋ง์ myStack์ ์์ธ ๊ฐ์ Key๋กํ๋ value๊ฐ l์ธ์ง ํ์ธ ํ ๋ค myStack์ ๋ง์ง๋ง ๊ฐ์ pop ํด์ค๋ค.
> 0์ด๊ฑฐ๋ ๋ง๋ value๊ฐ ์๋ ๊ฒฝ์ฐ invalid parentheses์ด๋ฏ๋ก false๋ฅผ ๋ฐ๋ก ๋ฆฌํดํด์ค๋ค.
foreach ๋ฐ๋ณต๋ฌธ์ด ๋๋ฌ์ ๋ myStack์ ๋จ์์๋ ๊ฐ์ด ์๋์ง ์ฒดํฌํ๊ณ ์๋ค๋ฉด true, ์๋ค๋ฉด false๋ฅผ ๋ฆฌํดํ๋ค.
ContainsKey O(1)์ด๊ณ , Stack์ insert(push)์ delete(pop) ๋ํ ๊ฐ๊ฐ O(1)์ด๋ฏ๋ก ์ ์๋ฃจ์ ์ time coplexity๋ O(n)์ด ๋๋ค.