From 78936251ec988b68fdebc1593b73eafcf911f582 Mon Sep 17 00:00:00 2001 From: wavty Date: Thu, 28 Sep 2023 18:25:43 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.0139 No.0139.Word Break --- solution/0100-0199/0139.Word Break/README.md | 18 ++++++++++++++++++ .../0100-0199/0139.Word Break/README_EN.md | 18 ++++++++++++++++++ solution/0100-0199/0139.Word Break/Solution.rs | 13 +++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 solution/0100-0199/0139.Word Break/Solution.rs diff --git a/solution/0100-0199/0139.Word Break/README.md b/solution/0100-0199/0139.Word Break/README.md index 94435beccd0b7..7266809652184 100644 --- a/solution/0100-0199/0139.Word Break/README.md +++ b/solution/0100-0199/0139.Word Break/README.md @@ -480,6 +480,24 @@ class Trie { } ``` +### **Rust** + +```rust +impl Solution { + pub fn word_break(s: String, word_dict: Vec) -> bool { + let words: std::collections::HashSet = word_dict.into_iter().collect(); + let mut f = vec![false; s.len() + 1]; + f[0] = true; + for i in 1..=s.len() { + for j in 0..i { + f[i] |= f[j] && words.contains(&s[j..i]); + } + } + f[s.len()] + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0139.Word Break/README_EN.md b/solution/0100-0199/0139.Word Break/README_EN.md index 0db2858571cf9..b17348dcf36ab 100644 --- a/solution/0100-0199/0139.Word Break/README_EN.md +++ b/solution/0100-0199/0139.Word Break/README_EN.md @@ -454,6 +454,24 @@ class Trie { } ``` +### **Rust** + +```rust +impl Solution { + pub fn word_break(s: String, word_dict: Vec) -> bool { + let words: std::collections::HashSet = word_dict.into_iter().collect(); + let mut f = vec![false; s.len() + 1]; + f[0] = true; + for i in 1..=s.len() { + for j in 0..i { + f[i] |= f[j] && words.contains(&s[j..i]); + } + } + f[s.len()] + } +} +``` + ### **...** ``` diff --git a/solution/0100-0199/0139.Word Break/Solution.rs b/solution/0100-0199/0139.Word Break/Solution.rs new file mode 100644 index 0000000000000..aea6722ada07d --- /dev/null +++ b/solution/0100-0199/0139.Word Break/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn word_break(s: String, word_dict: Vec) -> bool { + let words: std::collections::HashSet = word_dict.into_iter().collect(); + let mut f = vec![false; s.len() + 1]; + f[0] = true; + for i in 1..=s.len() { + for j in 0..i { + f[i] |= f[j] && words.contains(&s[j..i]); + } + } + f[s.len()] + } +}