Solution in C#


  • 0
    R
    public class Solution {
        public bool IsValid(string inputWord) {
            List<char> arr = inputWord.ToList();
                
                if(arr.Count == 1 && arr.Count == 0)
                {
                    return false;
                }
    
                for(int i = 0; i < arr.Count; i++)
                {
                    if(arr[i].Equals('}'))
                    {
                        if(i != 0 && arr[i - 1].Equals('{'))
                        {
                            arr.RemoveAt(i - 1);
                            arr.RemoveAt(i - 1);
                            i -= 2;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else if (arr[i].Equals(')'))
                    {
                        if (i != 0 && arr[i - 1].Equals('('))
                        {
                            arr.RemoveAt(i - 1);
                            arr.RemoveAt(i - 1);
                            i -= 2;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else if (arr[i].Equals(']'))
                    {
                        if (i != 0 && arr[i - 1].Equals('['))
                        {
                            arr.RemoveAt(i - 1);
                            arr.RemoveAt(i - 1);
                            i -= 2;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                
                if(arr.Count == 0)
                {
                    return true;
                }
                return false;
            }
        }
    '''

  • 0
    S
    public class Solution {
        public bool IsValid(string s) {
            
            Stack<char> stack = new Stack<char>();
            
            for(int i=0;i<s.Length;i++)
            {
                char c = s.ElementAt(i);
                
                if(isLeft(c))
                {
                    stack.Push(c);
                }
                else if(stack.Count > 0)
                {
                    char a = stack.Pop();
                    if(!isMatching(a, c))
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            
            if(stack.Count > 0)
            {
                return false;
            }
            return true;
        }
        
        private bool isLeft(char c)
        {
            return c == '(' || c == '{' || c== '[';
        }
        
        private bool isRight(char c)
        {
            return c == ')' || c == '}' || c == ']';
        }
        
        private bool isMatching(char c, char a)
        {
            return (c== '(' && a == ')') || (c == '{' && a == '}') || (c == '[' && a == ']');
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.