10 Ocak 2022 Pazartesi

LeetCode Question 20: Valid Parentheses - O(n) Complexity with Stack

public class Solution
{
    public bool IsValid(string input)
    {
        Stack<char> stack = new();

        if (input.Length == 1)
        {
            return false;
        }

        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] == '(')
            {
                stack.Push(')');
                continue;
            }

            if (input[i] == '{')
            {
                stack.Push('}');
                continue;
            }

            if (input[i] == '[')
            {
                stack.Push(']');
                continue;
            }

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

            var pop = stack.Pop();

            if (pop != input[i])
            {
                return false;
            }
        }

        if (stack.Count == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Hiç yorum yok:

Yorum Gönder