---
# System prepended metadata

title: Stack
tags: [Stack, Week_2]

---

###### tags: `Week_2`, `Stack`

# Stack

## 20. [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/submissions/)
Beny
(first try)
```csharp=
public class Solution {
    public bool IsValid(string s) {        
        Stack<char> stack = new Stack<char>();        
        for(int i = 0;i < s.Length; i++){
            switch(s[i]){
                case '(':                     
                case '[':  
                case '{':
                    stack.Push(s[i]);
                    break;
                case ')':
                    if(stack.Count == 0)
                        return false;
                    if('(' != stack.Pop())
                        return false;
                    break;
                case ']':  
                    if(stack.Count == 0)
                        return false;
                    if('[' != stack.Pop())
                        return false;
                    break;
                case '}':
                    if(stack.Count == 0)
                        return false;
                    if('{' != stack.Pop())
                        return false;
                    break;
            }   
        }
        if(stack.Count != 0){
            return false;
        }       
        return true;
    }
}
```
BENY
(second try)
```csharp=
public class Solution {
    public bool IsValid(string s) {        
        Stack<char> stack = new Stack<char>();        
        foreach(char c in s){
            switch(c){
                case '(':     
                    stack.Push(')');
                    break;
                case '[':
                    stack.Push(']');
                    break;
                case '{':
                    stack.Push('}');                    
                    break;
                default:
                    if(stack.Count == 0 || c != stack.Pop()){
                        return false;
                    }                    
                    break;
            }   
        }         
        return stack.Count == 0;
    }
}
```
## 844. [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)
Beny
(first try)
```csharp=
public class Solution {
    public bool BackspaceCompare(string s, string t) {
        
        Stack<char> sStack = new Stack<char>();
        Stack<char> tStack = new Stack<char>();
        foreach(char sc in s){
            if(sc == '#' && sStack.Count != 0){
                sStack.Pop();
            }else if(sc != '#'){
                sStack.Push(sc);
            }          
        }
        foreach(char tc in t){
            if(tc == '#' && tStack.Count !=0){
                tStack.Pop();
            }else if(tc != '#'){
                tStack.Push(tc);
            }
        }
        
        return Enumerable.SequenceEqual(sStack,tStack);
    }
}
```
## 844. [Backspace String Compare](https://leetcode.com/problems/backspace-string-compare/)
Beny
(first try)
```csharp=
public class Solution {
    public bool BackspaceCompare(string s, string t) {
        
        Stack<char> sStack = new Stack<char>();
        Stack<char> tStack = new Stack<char>();
        foreach(char sc in s){
            if(sc == '#' && sStack.Count != 0){
                sStack.Pop();
            }else if(sc != '#'){
                sStack.Push(sc);
            }          
        }
        foreach(char tc in t){
            if(tc == '#' && tStack.Count !=0){
                tStack.Pop();
            }else if(tc != '#'){
                tStack.Push(tc);
            }
        }
        
        return Enumerable.SequenceEqual(sStack,tStack);
    }
}
```
## 394. [Decode String](https://leetcode.com/problems/decode-string/?envType=study-plan&id=level-1)
Beny
```csharp=
public class Solution {
    public string DecodeString(string s) {
        string res = "";
        int curNum = 0;
        int num = 0;
        string temp = "";
        Stack<int> countStack = new Stack<int>();
        Stack<string> resStack = new Stack<string>();
        int idx = 0;
        foreach(char sc in s){
            if(sc == '['){
                resStack.Push(res);
                res = "";
                countStack.Push(curNum);
                curNum = 0;                
            }else if(sc == ']'){
                num = countStack.Pop();
                temp = resStack.Pop();
                string res1 = res;
                for(int i = 1; i < num; i++){
                    res += res1;
                }
                res = temp + res;
            }else if(Char.IsDigit(sc)){
                curNum = curNum*10 + (sc - '0'); //
            }else{
                res += sc;
            }            
        }
        return res;
    }   
}
```