Rust Programming Exercises: Matching Parenthesis

preview_player
Показать описание
Short video exercise on solving the common matching brackets problem. In this video we will use iterators over a string, using a vector as a stack, and the match syntax for controlling flow in Rust. These short exercises help reinforce common concepts in Rust and are well worth the time and effort to complete them in your own way.

↠ References:

Cheers! 🍻

🦀 #rust #rustlang #rustprogramming #exercises #leetcode #programming #algorithms
Рекомендации по теме
Комментарии
Автор

I don't know if you are taking requests for exercises but /if/ you are, an exercise that searches a body of text for certain pattern or tokens and then do some editing to that text found. Kinda like for every NAME change it to the person's name.

I have a project that I'm working on that I need to do text matching and replacement

joelmontesdeoca
Автор

This is LeetCode 20. I'm a Rust newbie, but I think you can simplify the code as follows:
pub fn is_valid(s: String) -> bool {
let brackets: HashMap<char, char> = vec![(')', '('), ('}', '{'), (']', '[')].into_iter().collect();
let opening_brackets: HashSet<char> =
let mut stack: Vec<char> = Vec::new();

for c in s.chars() {
if brackets.contains_key(&c) && stack.pop() != Some(brackets[&c]) {
return false;
} else if opening_brackets.contains(&c) {
stack.push(c);
}
}
stack.is_empty()
}
This runs at 0 ms, faster than 100.00% of Rust online submissions.

abhijit-sarkar
visit shbcf.ru