diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/README.md b/solution/0600-0699/0676.Implement Magic Dictionary/README.md index bdd8dbf2ccc66..4ce831461d15b 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/README.md +++ b/solution/0600-0699/0676.Implement Magic Dictionary/README.md @@ -431,124 +431,83 @@ class MagicDictionary { ```rust struct Trie { - children: Vec>>, - val: i32, + children: [Option>; 26], + is_end: bool, } impl Trie { fn new() -> Self { Trie { - children: (0..26).map(|_| None).collect(), - val: 0, + children: Default::default(), + is_end: false, } } - fn insert(&mut self, w: &str, x: i32) { + fn insert(&mut self, w: &str) { let mut node = self; for c in w.chars() { - let idx = (c as usize) - ('a' as usize); - if node.children[idx].is_none() { - node.children[idx] = Some(Box::new(Trie::new())); + let i = (c as usize) - ('a' as usize); + if node.children[i].is_none() { + node.children[i] = Some(Box::new(Trie::new())); } - node = node.children[idx].as_mut().unwrap(); - node.val += x; + node = node.children[i].as_mut().unwrap(); } + node.is_end = true; } - fn search(&self, w: &str) -> i32 { - let mut node = self; - for c in w.chars() { - let idx = (c as usize) - ('a' as usize); - if node.children[idx].is_none() { - return 0; - } - node = node.children[idx].as_ref().unwrap(); - } - node.val + fn search(&self, w: &str) -> bool { + self.dfs(w, 0, 0) } -} -struct MapSum { - d: std::collections::HashMap, - trie: Trie, -} - -impl MapSum { - fn new() -> Self { - MapSum { - d: std::collections::HashMap::new(), - trie: Trie::new(), + fn dfs(&self, w: &str, i: usize, diff: usize) -> bool { + if i == w.len() { + return diff == 1 && self.is_end; } - } - - fn insert(&mut self, key: String, val: i32) { - let x = val - self.d.get(&key).unwrap_or(&0); - self.d.insert(key.clone(), val); - self.trie.insert(&key, x); - } - fn sum(&self, prefix: String) -> i32 { - self.trie.search(&prefix) - } -} -``` - -#### C# - -```cs -public class Trie { - private Trie[] children = new Trie[26]; - private int val; + let j = (w.chars().nth(i).unwrap() as usize) - ('a' as usize); - public void Insert(string w, int x) { - Trie node = this; - for (int i = 0; i < w.Length; ++i) { - int idx = w[i] - 'a'; - if (node.children[idx] == null) { - node.children[idx] = new Trie(); + if let Some(child) = &self.children[j] { + if child.dfs(w, i + 1, diff) { + return true; } - node = node.children[idx]; - node.val += x; } - } - public int Search(string w) { - Trie node = this; - for (int i = 0; i < w.Length; ++i) { - int idx = w[i] - 'a'; - if (node.children[idx] == null) { - return 0; + if diff == 0 { + for k in 0..26 { + if k != j { + if let Some(child) = &self.children[k] { + if child.dfs(w, i + 1, 1) { + return true; + } + } + } } - node = node.children[idx]; } - return node.val; + false } } -public class MapSum { - private Dictionary d = new Dictionary(); - private Trie trie = new Trie(); +struct MagicDictionary { + trie: Trie, +} - public MapSum() { +impl MagicDictionary { + fn new() -> Self { + MagicDictionary { + trie: Trie::new(), + } } - public void Insert(string key, int val) { - int x = val - (d.ContainsKey(key) ? d[key] : 0); - d[key] = val; - trie.Insert(key, x); + fn build_dict(&mut self, dictionary: Vec) { + for w in dictionary { + self.trie.insert(&w); + } } - public int Sum(string prefix) { - return trie.Search(prefix); + fn search(&self, search_word: String) -> bool { + self.trie.search(&search_word) } } - -/** - * Your MapSum object will be instantiated and called as such: - * MapSum obj = new MapSum(); - * obj.Insert(key,val); - * int param_2 = obj.Sum(prefix); - */ ``` diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md b/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md index 152d13cea2e53..649b6b231448e 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md +++ b/solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md @@ -423,124 +423,83 @@ class MagicDictionary { ```rust struct Trie { - children: Vec>>, - val: i32, + children: [Option>; 26], + is_end: bool, } impl Trie { fn new() -> Self { Trie { - children: (0..26).map(|_| None).collect(), - val: 0, + children: Default::default(), + is_end: false, } } - fn insert(&mut self, w: &str, x: i32) { + fn insert(&mut self, w: &str) { let mut node = self; for c in w.chars() { - let idx = (c as usize) - ('a' as usize); - if node.children[idx].is_none() { - node.children[idx] = Some(Box::new(Trie::new())); + let i = (c as usize) - ('a' as usize); + if node.children[i].is_none() { + node.children[i] = Some(Box::new(Trie::new())); } - node = node.children[idx].as_mut().unwrap(); - node.val += x; + node = node.children[i].as_mut().unwrap(); } + node.is_end = true; } - fn search(&self, w: &str) -> i32 { - let mut node = self; - for c in w.chars() { - let idx = (c as usize) - ('a' as usize); - if node.children[idx].is_none() { - return 0; - } - node = node.children[idx].as_ref().unwrap(); - } - node.val + fn search(&self, w: &str) -> bool { + self.dfs(w, 0, 0) } -} -struct MapSum { - d: std::collections::HashMap, - trie: Trie, -} - -impl MapSum { - fn new() -> Self { - MapSum { - d: std::collections::HashMap::new(), - trie: Trie::new(), + fn dfs(&self, w: &str, i: usize, diff: usize) -> bool { + if i == w.len() { + return diff == 1 && self.is_end; } - } - - fn insert(&mut self, key: String, val: i32) { - let x = val - self.d.get(&key).unwrap_or(&0); - self.d.insert(key.clone(), val); - self.trie.insert(&key, x); - } - fn sum(&self, prefix: String) -> i32 { - self.trie.search(&prefix) - } -} -``` - -#### C# - -```cs -public class Trie { - private Trie[] children = new Trie[26]; - private int val; + let j = (w.chars().nth(i).unwrap() as usize) - ('a' as usize); - public void Insert(string w, int x) { - Trie node = this; - for (int i = 0; i < w.Length; ++i) { - int idx = w[i] - 'a'; - if (node.children[idx] == null) { - node.children[idx] = new Trie(); + if let Some(child) = &self.children[j] { + if child.dfs(w, i + 1, diff) { + return true; } - node = node.children[idx]; - node.val += x; } - } - public int Search(string w) { - Trie node = this; - for (int i = 0; i < w.Length; ++i) { - int idx = w[i] - 'a'; - if (node.children[idx] == null) { - return 0; + if diff == 0 { + for k in 0..26 { + if k != j { + if let Some(child) = &self.children[k] { + if child.dfs(w, i + 1, 1) { + return true; + } + } + } } - node = node.children[idx]; } - return node.val; + false } } -public class MapSum { - private Dictionary d = new Dictionary(); - private Trie trie = new Trie(); +struct MagicDictionary { + trie: Trie, +} - public MapSum() { +impl MagicDictionary { + fn new() -> Self { + MagicDictionary { + trie: Trie::new(), + } } - public void Insert(string key, int val) { - int x = val - (d.ContainsKey(key) ? d[key] : 0); - d[key] = val; - trie.Insert(key, x); + fn build_dict(&mut self, dictionary: Vec) { + for w in dictionary { + self.trie.insert(&w); + } } - public int Sum(string prefix) { - return trie.Search(prefix); + fn search(&self, search_word: String) -> bool { + self.trie.search(&search_word) } } - -/** - * Your MapSum object will be instantiated and called as such: - * MapSum obj = new MapSum(); - * obj.Insert(key,val); - * int param_2 = obj.Sum(prefix); - */ ``` diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.rs b/solution/0600-0699/0676.Implement Magic Dictionary/Solution.rs index baac4b318e933..55c42ecb0e9dd 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.rs +++ b/solution/0600-0699/0676.Implement Magic Dictionary/Solution.rs @@ -1,61 +1,76 @@ struct Trie { - children: Vec>>, - val: i32, + children: [Option>; 26], + is_end: bool, } impl Trie { fn new() -> Self { Trie { - children: (0..26).map(|_| None).collect(), - val: 0, + children: Default::default(), + is_end: false, } } - fn insert(&mut self, w: &str, x: i32) { + fn insert(&mut self, w: &str) { let mut node = self; for c in w.chars() { - let idx = (c as usize) - ('a' as usize); - if node.children[idx].is_none() { - node.children[idx] = Some(Box::new(Trie::new())); + let i = (c as usize) - ('a' as usize); + if node.children[i].is_none() { + node.children[i] = Some(Box::new(Trie::new())); } - node = node.children[idx].as_mut().unwrap(); - node.val += x; + node = node.children[i].as_mut().unwrap(); } + node.is_end = true; } - fn search(&self, w: &str) -> i32 { - let mut node = self; - for c in w.chars() { - let idx = (c as usize) - ('a' as usize); - if node.children[idx].is_none() { - return 0; + fn search(&self, w: &str) -> bool { + self.dfs(w, 0, 0) + } + + fn dfs(&self, w: &str, i: usize, diff: usize) -> bool { + if i == w.len() { + return diff == 1 && self.is_end; + } + + let j = (w.chars().nth(i).unwrap() as usize) - ('a' as usize); + + if let Some(child) = &self.children[j] { + if child.dfs(w, i + 1, diff) { + return true; + } + } + + if diff == 0 { + for k in 0..26 { + if k != j { + if let Some(child) = &self.children[k] { + if child.dfs(w, i + 1, 1) { + return true; + } + } + } } - node = node.children[idx].as_ref().unwrap(); } - node.val + false } } -struct MapSum { - d: std::collections::HashMap, +struct MagicDictionary { trie: Trie, } -impl MapSum { +impl MagicDictionary { fn new() -> Self { - MapSum { - d: std::collections::HashMap::new(), - trie: Trie::new(), - } + MagicDictionary { trie: Trie::new() } } - fn insert(&mut self, key: String, val: i32) { - let x = val - self.d.get(&key).unwrap_or(&0); - self.d.insert(key.clone(), val); - self.trie.insert(&key, x); + fn build_dict(&mut self, dictionary: Vec) { + for w in dictionary { + self.trie.insert(&w); + } } - fn sum(&self, prefix: String) -> i32 { - self.trie.search(&prefix) + fn search(&self, search_word: String) -> bool { + self.trie.search(&search_word) } } diff --git a/solution/0600-0699/0677.Map Sum Pairs/README.md b/solution/0600-0699/0677.Map Sum Pairs/README.md index b72f07706836c..3d6d4d7790438 100644 --- a/solution/0600-0699/0677.Map Sum Pairs/README.md +++ b/solution/0600-0699/0677.Map Sum Pairs/README.md @@ -379,6 +379,72 @@ class MapSum { */ ``` +#### Rust + +```rust +struct Trie { + children: Vec>>, + val: i32, +} + +impl Trie { + fn new() -> Self { + Trie { + children: (0..26).map(|_| None).collect(), + val: 0, + } + } + + fn insert(&mut self, w: &str, x: i32) { + let mut node = self; + for c in w.chars() { + let idx = (c as usize) - ('a' as usize); + if node.children[idx].is_none() { + node.children[idx] = Some(Box::new(Trie::new())); + } + node = node.children[idx].as_mut().unwrap(); + node.val += x; + } + } + + fn search(&self, w: &str) -> i32 { + let mut node = self; + for c in w.chars() { + let idx = (c as usize) - ('a' as usize); + if node.children[idx].is_none() { + return 0; + } + node = node.children[idx].as_ref().unwrap(); + } + node.val + } +} + +struct MapSum { + d: std::collections::HashMap, + trie: Trie, +} + +impl MapSum { + fn new() -> Self { + MapSum { + d: std::collections::HashMap::new(), + trie: Trie::new(), + } + } + + fn insert(&mut self, key: String, val: i32) { + let x = val - self.d.get(&key).unwrap_or(&0); + self.d.insert(key.clone(), val); + self.trie.insert(&key, x); + } + + fn sum(&self, prefix: String) -> i32 { + self.trie.search(&prefix) + } +} +``` + #### JavaScript ```js @@ -445,6 +511,64 @@ MapSum.prototype.sum = function (prefix) { */ ``` +#### C# + +```cs +public class Trie { + private Trie[] children = new Trie[26]; + private int val; + + public void Insert(string w, int x) { + Trie node = this; + for (int i = 0; i < w.Length; ++i) { + int idx = w[i] - 'a'; + if (node.children[idx] == null) { + node.children[idx] = new Trie(); + } + node = node.children[idx]; + node.val += x; + } + } + + public int Search(string w) { + Trie node = this; + for (int i = 0; i < w.Length; ++i) { + int idx = w[i] - 'a'; + if (node.children[idx] == null) { + return 0; + } + node = node.children[idx]; + } + return node.val; + } +} + +public class MapSum { + private Dictionary d = new Dictionary(); + private Trie trie = new Trie(); + + public MapSum() { + } + + public void Insert(string key, int val) { + int x = val - (d.ContainsKey(key) ? d[key] : 0); + d[key] = val; + trie.Insert(key, x); + } + + public int Sum(string prefix) { + return trie.Search(prefix); + } +} + +/** + * Your MapSum object will be instantiated and called as such: + * MapSum obj = new MapSum(); + * obj.Insert(key,val); + * int param_2 = obj.Sum(prefix); + */ +``` + diff --git a/solution/0600-0699/0677.Map Sum Pairs/README_EN.md b/solution/0600-0699/0677.Map Sum Pairs/README_EN.md index 3c1b9c409295f..0627ece6e63be 100644 --- a/solution/0600-0699/0677.Map Sum Pairs/README_EN.md +++ b/solution/0600-0699/0677.Map Sum Pairs/README_EN.md @@ -377,6 +377,72 @@ class MapSum { */ ``` +#### Rust + +```rust +struct Trie { + children: Vec>>, + val: i32, +} + +impl Trie { + fn new() -> Self { + Trie { + children: (0..26).map(|_| None).collect(), + val: 0, + } + } + + fn insert(&mut self, w: &str, x: i32) { + let mut node = self; + for c in w.chars() { + let idx = (c as usize) - ('a' as usize); + if node.children[idx].is_none() { + node.children[idx] = Some(Box::new(Trie::new())); + } + node = node.children[idx].as_mut().unwrap(); + node.val += x; + } + } + + fn search(&self, w: &str) -> i32 { + let mut node = self; + for c in w.chars() { + let idx = (c as usize) - ('a' as usize); + if node.children[idx].is_none() { + return 0; + } + node = node.children[idx].as_ref().unwrap(); + } + node.val + } +} + +struct MapSum { + d: std::collections::HashMap, + trie: Trie, +} + +impl MapSum { + fn new() -> Self { + MapSum { + d: std::collections::HashMap::new(), + trie: Trie::new(), + } + } + + fn insert(&mut self, key: String, val: i32) { + let x = val - self.d.get(&key).unwrap_or(&0); + self.d.insert(key.clone(), val); + self.trie.insert(&key, x); + } + + fn sum(&self, prefix: String) -> i32 { + self.trie.search(&prefix) + } +} +``` + #### JavaScript ```js @@ -443,6 +509,64 @@ MapSum.prototype.sum = function (prefix) { */ ``` +#### C# + +```cs +public class Trie { + private Trie[] children = new Trie[26]; + private int val; + + public void Insert(string w, int x) { + Trie node = this; + for (int i = 0; i < w.Length; ++i) { + int idx = w[i] - 'a'; + if (node.children[idx] == null) { + node.children[idx] = new Trie(); + } + node = node.children[idx]; + node.val += x; + } + } + + public int Search(string w) { + Trie node = this; + for (int i = 0; i < w.Length; ++i) { + int idx = w[i] - 'a'; + if (node.children[idx] == null) { + return 0; + } + node = node.children[idx]; + } + return node.val; + } +} + +public class MapSum { + private Dictionary d = new Dictionary(); + private Trie trie = new Trie(); + + public MapSum() { + } + + public void Insert(string key, int val) { + int x = val - (d.ContainsKey(key) ? d[key] : 0); + d[key] = val; + trie.Insert(key, x); + } + + public int Sum(string prefix) { + return trie.Search(prefix); + } +} + +/** + * Your MapSum object will be instantiated and called as such: + * MapSum obj = new MapSum(); + * obj.Insert(key,val); + * int param_2 = obj.Sum(prefix); + */ +``` + diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.cs b/solution/0600-0699/0677.Map Sum Pairs/Solution.cs similarity index 100% rename from solution/0600-0699/0676.Implement Magic Dictionary/Solution.cs rename to solution/0600-0699/0677.Map Sum Pairs/Solution.cs diff --git a/solution/0600-0699/0677.Map Sum Pairs/Solution.rs b/solution/0600-0699/0677.Map Sum Pairs/Solution.rs new file mode 100644 index 0000000000000..baac4b318e933 --- /dev/null +++ b/solution/0600-0699/0677.Map Sum Pairs/Solution.rs @@ -0,0 +1,61 @@ +struct Trie { + children: Vec>>, + val: i32, +} + +impl Trie { + fn new() -> Self { + Trie { + children: (0..26).map(|_| None).collect(), + val: 0, + } + } + + fn insert(&mut self, w: &str, x: i32) { + let mut node = self; + for c in w.chars() { + let idx = (c as usize) - ('a' as usize); + if node.children[idx].is_none() { + node.children[idx] = Some(Box::new(Trie::new())); + } + node = node.children[idx].as_mut().unwrap(); + node.val += x; + } + } + + fn search(&self, w: &str) -> i32 { + let mut node = self; + for c in w.chars() { + let idx = (c as usize) - ('a' as usize); + if node.children[idx].is_none() { + return 0; + } + node = node.children[idx].as_ref().unwrap(); + } + node.val + } +} + +struct MapSum { + d: std::collections::HashMap, + trie: Trie, +} + +impl MapSum { + fn new() -> Self { + MapSum { + d: std::collections::HashMap::new(), + trie: Trie::new(), + } + } + + fn insert(&mut self, key: String, val: i32) { + let x = val - self.d.get(&key).unwrap_or(&0); + self.d.insert(key.clone(), val); + self.trie.insert(&key, x); + } + + fn sum(&self, prefix: String) -> i32 { + self.trie.search(&prefix) + } +}