Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problem: No.0676 #4275

Merged
merged 1 commit into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 88 additions & 43 deletions solution/0600-0699/0676.Implement Magic Dictionary/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -430,80 +430,125 @@ class MagicDictionary {
#### Rust

```rust
use std::collections::HashMap;

#[derive(Clone)]
struct Trie {
children: Vec<Option<Box<Trie>>>,
is_end: bool,
val: i32,
}

impl Trie {
fn new() -> Self {
Trie {
children: vec![None; 26],
is_end: false,
children: (0..26).map(|_| None).collect(),
val: 0,
}
}

fn insert(&mut self, word: &str) {
fn insert(&mut self, w: &str, x: i32) {
let mut node = self;
for &ch in word.as_bytes() {
let index = (ch - b'a') as usize;
node = node.children[index].get_or_insert_with(|| Box::new(Trie::new()));
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;
}
node.is_end = true;
}

fn search(&self, word: &str, diff: i32) -> bool {
if word.is_empty() {
return diff == 1 && self.is_end;
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
}
}

let index = (word.as_bytes()[0] - b'a') as usize;
if let Some(child) = &self.children[index] {
if child.search(&word[1..], diff) {
return true;
}
struct MapSum {
d: std::collections::HashMap<String, i32>,
trie: Trie,
}

impl MapSum {
fn new() -> Self {
MapSum {
d: std::collections::HashMap::new(),
trie: Trie::new(),
}
}

if diff == 0 {
for (i, child) in self.children.iter().enumerate() {
if i != index && child.is_some() {
if child.as_ref().unwrap().search(&word[1..], 1) {
return true;
}
}
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;

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;
}
}

false
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;
}
}

struct MagicDictionary {
trie: Trie,
}
public class MapSum {
private Dictionary<string, int> d = new Dictionary<string, int>();
private Trie trie = new Trie();

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MagicDictionary {
fn new() -> Self {
MagicDictionary { trie: Trie::new() }
public MapSum() {
}

fn build_dict(&mut self, dictionary: Vec<String>) {
for word in dictionary {
self.trie.insert(&word);
}
public void Insert(string key, int val) {
int x = val - (d.ContainsKey(key) ? d[key] : 0);
d[key] = val;
trie.Insert(key, x);
}

fn search(&self, search_word: String) -> bool {
self.trie.search(&search_word, 0)
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);
*/
```

<!-- tabs:end -->
Expand Down
131 changes: 88 additions & 43 deletions solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,80 +422,125 @@ class MagicDictionary {
#### Rust

```rust
use std::collections::HashMap;

#[derive(Clone)]
struct Trie {
children: Vec<Option<Box<Trie>>>,
is_end: bool,
val: i32,
}

impl Trie {
fn new() -> Self {
Trie {
children: vec![None; 26],
is_end: false,
children: (0..26).map(|_| None).collect(),
val: 0,
}
}

fn insert(&mut self, word: &str) {
fn insert(&mut self, w: &str, x: i32) {
let mut node = self;
for &ch in word.as_bytes() {
let index = (ch - b'a') as usize;
node = node.children[index].get_or_insert_with(|| Box::new(Trie::new()));
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;
}
node.is_end = true;
}

fn search(&self, word: &str, diff: i32) -> bool {
if word.is_empty() {
return diff == 1 && self.is_end;
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
}
}

let index = (word.as_bytes()[0] - b'a') as usize;
if let Some(child) = &self.children[index] {
if child.search(&word[1..], diff) {
return true;
}
struct MapSum {
d: std::collections::HashMap<String, i32>,
trie: Trie,
}

impl MapSum {
fn new() -> Self {
MapSum {
d: std::collections::HashMap::new(),
trie: Trie::new(),
}
}

if diff == 0 {
for (i, child) in self.children.iter().enumerate() {
if i != index && child.is_some() {
if child.as_ref().unwrap().search(&word[1..], 1) {
return true;
}
}
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;

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;
}
}

false
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;
}
}

struct MagicDictionary {
trie: Trie,
}
public class MapSum {
private Dictionary<string, int> d = new Dictionary<string, int>();
private Trie trie = new Trie();

/**
* `&self` means the method takes an immutable reference.
* If you need a mutable reference, change it to `&mut self` instead.
*/
impl MagicDictionary {
fn new() -> Self {
MagicDictionary { trie: Trie::new() }
public MapSum() {
}

fn build_dict(&mut self, dictionary: Vec<String>) {
for word in dictionary {
self.trie.insert(&word);
}
public void Insert(string key, int val) {
int x = val - (d.ContainsKey(key) ? d[key] : 0);
d[key] = val;
trie.Insert(key, x);
}

fn search(&self, search_word: String) -> bool {
self.trie.search(&search_word, 0)
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);
*/
```

<!-- tabs:end -->
Expand Down
53 changes: 53 additions & 0 deletions solution/0600-0699/0676.Implement Magic Dictionary/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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<string, int> d = new Dictionary<string, int>();
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);
*/
Loading
Loading