1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//! Valid Parentheses [leetcode: valid_parentheses](https://leetcode.com/problems/valid-parentheses/)
//!
//! Given a string containing just the characters `'('`, `')'`, `'{'`, `'}'`, `'['` and `']'`, determine if the input string is valid.
//!
//! An input string is valid if:
//!
//! 1. Open brackets must be closed by the same type of brackets.
//! 2. Open brackets must be closed in the correct order.
//!
//! Note that an empty string is also considered valid.
//!
//! ***Example1:***
//!
//! ```
//! Input: "()"
//!
//! Output: true
//! ```
//!
//! ***Example2:***
//!
//! ```
//! Input: "()[]{}"
//!
//! Output: true
//! ```
//!
//! ***Example3:***
//!
//! ```
//! Input: "(]"
//!
//! Output: false
//! ```
//!
//! ***Example4:***
//!
//! ```
//! Input: "([)]"
//!
//! Output: false
//! ```
//!
//! ***Example5:***
//!
//! ```
//! Input: "{[]}"
//!
//! Output: true
//! ```

use std::collections::HashMap;
/// # Solutions
///
/// # Approach 1: Stacks
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// ```rust
/// use std::collections::HashMap;
///
/// impl Solution {
///     pub fn is_valid(s: String) -> bool {
///         let mut v = vec![];
///         let mut str_map: HashMap<char, char> = HashMap::new();
///         str_map.insert(')', '(');
///         str_map.insert('}', '{');
///         str_map.insert(']', '[');
///
///         for s1 in s.chars() {
///             if str_map.contains_key(&s1) {
///                 if v.pop() != Some(str_map[&s1]) {
///                     return false;
///                 }
///             } else {
///                 v.push(s1)
///             }
///         }
///
///         v.is_empty()
///     }
/// }
/// ```
///
/// # Approach 2: Stacks
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// ```rust
/// impl Solution {
///     pub fn is_valid(s: String) -> bool {
///         let mut stack = vec![];
///         for c in s.chars() {
///             match c {
///                 '(' => stack.push(')'),
///                 '{' => stack.push('}'),
///                 '[' => stack.push(']'),
///                 ')' | '}' | ']' => {
///                     match stack.pop() {
///                         None => return false,
///                         Some(ch) => { if ch != c { return false; }},
///                     }
///                 },
///                 _ => {},
///             }
///         }
///         stack.is_empty()
///     }
/// }
/// ```
///
pub fn is_valid(s: String) -> bool {
    let mut v = vec![];
    let mut str_map: HashMap<char, char> = HashMap::new();
    str_map.insert(')', '(');
    str_map.insert('}', '{');
    str_map.insert(']', '[');

    for s1 in s.chars() {
        if str_map.contains_key(&s1) {
            if v.pop() != Some(str_map[&s1]) {
                return false;
            }
        } else {
            v.push(s1)
        }
    }

    v.is_empty()
}

/// # Solutions
///
/// # Approach 1: Stacks
///
/// * Time complexity: O(n)
///
/// * Space complexity: O(n)
///
/// ```rust
/// use std::collections::HashMap;
///
/// impl Solution {
///     pub fn is_valid2(s: String) -> bool {
///         let mut paren_stack = vec![];
///         let mut paren_map: HashMap<char, char> = HashMap::new();
///         paren_map.insert(')', '(');
///         paren_map.insert('}', '{');
///         paren_map.insert(']', '[');
///
///         for ch in s.chars() {
///             match ch {
///                 '(' | '{' | '[' => paren_stack.push(ch),
///                 _ => {
///                     let expected = *paren_map.get(&ch).unwrap();
///                     let actual = paren_stack.pop().unwrap();
///                     if expected != actual {
///                         return false;
///                     }
///                 }
///             }
///         }
///
///         paren_stack.is_empty()
///     }
/// }
/// ```
///
pub fn is_valid2(s: String) -> bool {
    let mut paren_stack = vec![];
    let mut paren_map: HashMap<char, char> = HashMap::new();
    paren_map.insert(')', '(');
    paren_map.insert('}', '{');
    paren_map.insert(']', '[');

    for ch in s.chars() {
        match ch {
            '(' | '{' | '[' => paren_stack.push(ch),
            _ => {
                let expected = *paren_map.get(&ch).unwrap();
                let actual = paren_stack.pop().unwrap();
                if expected != actual {
                    return false;
                }
            }
        }
    }

    paren_stack.is_empty()
}