Problem solving/Algorithms

[LeetCode] 20. Valid Parentheses (C#)

Young_A 2021. 1. 26. 11:22

๋ชฉ์ฐจ

    LeetCode - Problems - Algorithms - 20. Valid Parentheses

     

    Problem Description

    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.

    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 '()[]{}'.

    2019๋…„ 12์›” ์ˆ˜๋งŽ์€ ์‹œ๋„๋“ค.. ๊ทธ๋ฆฌ๊ณ  ํฌ๊ธฐ

    ์ด ๋ฌธ์ œ๋Š” ์žฌ์ž‘๋…„ 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์„ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ด์•ผ์ง€.

     

    LeetCode - 20. Valid Parentheses

    ++์ถ”๊ฐ€

    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)์ด ๋œ๋‹ค.

    LeetCode - 20. Valid Parentheses - Using Stack

    'Problem solving > Algorithms' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [LeetCode] 26. Remove Duplicates from Sorted Array (C#)  (0) 2021.01.29
    [LeetCode] 21. Merge Two Sorted Lists (C#)  (0) 2021.01.29
    [LeetCode] 14. Longest Common Prefix (C#)  (0) 2021.01.25
    [LeetCode] 13. Roman to Integer (C#)  (0) 2021.01.24
    [LeetCode] 9. Palindrome Number (C#)  (0) 2021.01.23