Skip to main content

rustgym/leetcode/
_844_backspace_string_compare.rs

1struct Solution;
2
3impl Solution {
4    fn backspace_compare(s: String, t: String) -> bool {
5        let mut ss: Vec<char> = vec![];
6        let mut ts: Vec<char> = vec![];
7        for c in s.chars() {
8            if c == '#' {
9                ss.pop();
10            } else {
11                ss.push(c);
12            }
13        }
14        for c in t.chars() {
15            if c == '#' {
16                ts.pop();
17            } else {
18                ts.push(c);
19            }
20        }
21        ss == ts
22    }
23}
24
25#[test]
26fn test() {
27    assert_eq!(
28        Solution::backspace_compare("ab#c".to_string(), "ad#c".to_string()),
29        true
30    );
31    assert_eq!(
32        Solution::backspace_compare("ab##".to_string(), "c#d#".to_string()),
33        true
34    );
35    assert_eq!(
36        Solution::backspace_compare("a##c".to_string(), "#a#c".to_string()),
37        true
38    );
39    assert_eq!(
40        Solution::backspace_compare("a#c".to_string(), "b".to_string()),
41        false
42    );
43}