Skip to content

Commit 479a4b2

Browse files
authored
feat: add solutions to lc problem: No.0676 (#4275)
1 parent 5158b03 commit 479a4b2

File tree

7 files changed

+468
-132
lines changed

7 files changed

+468
-132
lines changed

solution/0600-0699/0676.Implement Magic Dictionary/README.md

+88-43
Original file line numberDiff line numberDiff line change
@@ -430,80 +430,125 @@ class MagicDictionary {
430430
#### Rust
431431

432432
```rust
433-
use std::collections::HashMap;
434-
435-
#[derive(Clone)]
436433
struct Trie {
437434
children: Vec<Option<Box<Trie>>>,
438-
is_end: bool,
435+
val: i32,
439436
}
440437

441438
impl Trie {
442439
fn new() -> Self {
443440
Trie {
444-
children: vec![None; 26],
445-
is_end: false,
441+
children: (0..26).map(|_| None).collect(),
442+
val: 0,
446443
}
447444
}
448445

449-
fn insert(&mut self, word: &str) {
446+
fn insert(&mut self, w: &str, x: i32) {
450447
let mut node = self;
451-
for &ch in word.as_bytes() {
452-
let index = (ch - b'a') as usize;
453-
node = node.children[index].get_or_insert_with(|| Box::new(Trie::new()));
448+
for c in w.chars() {
449+
let idx = (c as usize) - ('a' as usize);
450+
if node.children[idx].is_none() {
451+
node.children[idx] = Some(Box::new(Trie::new()));
452+
}
453+
node = node.children[idx].as_mut().unwrap();
454+
node.val += x;
454455
}
455-
node.is_end = true;
456456
}
457457

458-
fn search(&self, word: &str, diff: i32) -> bool {
459-
if word.is_empty() {
460-
return diff == 1 && self.is_end;
458+
fn search(&self, w: &str) -> i32 {
459+
let mut node = self;
460+
for c in w.chars() {
461+
let idx = (c as usize) - ('a' as usize);
462+
if node.children[idx].is_none() {
463+
return 0;
464+
}
465+
node = node.children[idx].as_ref().unwrap();
461466
}
467+
node.val
468+
}
469+
}
462470

463-
let index = (word.as_bytes()[0] - b'a') as usize;
464-
if let Some(child) = &self.children[index] {
465-
if child.search(&word[1..], diff) {
466-
return true;
467-
}
471+
struct MapSum {
472+
d: std::collections::HashMap<String, i32>,
473+
trie: Trie,
474+
}
475+
476+
impl MapSum {
477+
fn new() -> Self {
478+
MapSum {
479+
d: std::collections::HashMap::new(),
480+
trie: Trie::new(),
468481
}
482+
}
469483

470-
if diff == 0 {
471-
for (i, child) in self.children.iter().enumerate() {
472-
if i != index && child.is_some() {
473-
if child.as_ref().unwrap().search(&word[1..], 1) {
474-
return true;
475-
}
476-
}
484+
fn insert(&mut self, key: String, val: i32) {
485+
let x = val - self.d.get(&key).unwrap_or(&0);
486+
self.d.insert(key.clone(), val);
487+
self.trie.insert(&key, x);
488+
}
489+
490+
fn sum(&self, prefix: String) -> i32 {
491+
self.trie.search(&prefix)
492+
}
493+
}
494+
```
495+
496+
#### C#
497+
498+
```cs
499+
public class Trie {
500+
private Trie[] children = new Trie[26];
501+
private int val;
502+
503+
public void Insert(string w, int x) {
504+
Trie node = this;
505+
for (int i = 0; i < w.Length; ++i) {
506+
int idx = w[i] - 'a';
507+
if (node.children[idx] == null) {
508+
node.children[idx] = new Trie();
477509
}
510+
node = node.children[idx];
511+
node.val += x;
478512
}
513+
}
479514

480-
false
515+
public int Search(string w) {
516+
Trie node = this;
517+
for (int i = 0; i < w.Length; ++i) {
518+
int idx = w[i] - 'a';
519+
if (node.children[idx] == null) {
520+
return 0;
521+
}
522+
node = node.children[idx];
523+
}
524+
return node.val;
481525
}
482526
}
483527

484-
struct MagicDictionary {
485-
trie: Trie,
486-
}
528+
public class MapSum {
529+
private Dictionary<string, int> d = new Dictionary<string, int>();
530+
private Trie trie = new Trie();
487531

488-
/**
489-
* `&self` means the method takes an immutable reference.
490-
* If you need a mutable reference, change it to `&mut self` instead.
491-
*/
492-
impl MagicDictionary {
493-
fn new() -> Self {
494-
MagicDictionary { trie: Trie::new() }
532+
public MapSum() {
495533
}
496534

497-
fn build_dict(&mut self, dictionary: Vec<String>) {
498-
for word in dictionary {
499-
self.trie.insert(&word);
500-
}
535+
public void Insert(string key, int val) {
536+
int x = val - (d.ContainsKey(key) ? d[key] : 0);
537+
d[key] = val;
538+
trie.Insert(key, x);
501539
}
502540

503-
fn search(&self, search_word: String) -> bool {
504-
self.trie.search(&search_word, 0)
541+
public int Sum(string prefix) {
542+
return trie.Search(prefix);
505543
}
506544
}
545+
546+
/**
547+
* Your MapSum object will be instantiated and called as such:
548+
* MapSum obj = new MapSum();
549+
* obj.Insert(key,val);
550+
* int param_2 = obj.Sum(prefix);
551+
*/
507552
```
508553

509554
<!-- tabs:end -->

solution/0600-0699/0676.Implement Magic Dictionary/README_EN.md

+88-43
Original file line numberDiff line numberDiff line change
@@ -422,80 +422,125 @@ class MagicDictionary {
422422
#### Rust
423423

424424
```rust
425-
use std::collections::HashMap;
426-
427-
#[derive(Clone)]
428425
struct Trie {
429426
children: Vec<Option<Box<Trie>>>,
430-
is_end: bool,
427+
val: i32,
431428
}
432429

433430
impl Trie {
434431
fn new() -> Self {
435432
Trie {
436-
children: vec![None; 26],
437-
is_end: false,
433+
children: (0..26).map(|_| None).collect(),
434+
val: 0,
438435
}
439436
}
440437

441-
fn insert(&mut self, word: &str) {
438+
fn insert(&mut self, w: &str, x: i32) {
442439
let mut node = self;
443-
for &ch in word.as_bytes() {
444-
let index = (ch - b'a') as usize;
445-
node = node.children[index].get_or_insert_with(|| Box::new(Trie::new()));
440+
for c in w.chars() {
441+
let idx = (c as usize) - ('a' as usize);
442+
if node.children[idx].is_none() {
443+
node.children[idx] = Some(Box::new(Trie::new()));
444+
}
445+
node = node.children[idx].as_mut().unwrap();
446+
node.val += x;
446447
}
447-
node.is_end = true;
448448
}
449449

450-
fn search(&self, word: &str, diff: i32) -> bool {
451-
if word.is_empty() {
452-
return diff == 1 && self.is_end;
450+
fn search(&self, w: &str) -> i32 {
451+
let mut node = self;
452+
for c in w.chars() {
453+
let idx = (c as usize) - ('a' as usize);
454+
if node.children[idx].is_none() {
455+
return 0;
456+
}
457+
node = node.children[idx].as_ref().unwrap();
453458
}
459+
node.val
460+
}
461+
}
454462

455-
let index = (word.as_bytes()[0] - b'a') as usize;
456-
if let Some(child) = &self.children[index] {
457-
if child.search(&word[1..], diff) {
458-
return true;
459-
}
463+
struct MapSum {
464+
d: std::collections::HashMap<String, i32>,
465+
trie: Trie,
466+
}
467+
468+
impl MapSum {
469+
fn new() -> Self {
470+
MapSum {
471+
d: std::collections::HashMap::new(),
472+
trie: Trie::new(),
460473
}
474+
}
461475

462-
if diff == 0 {
463-
for (i, child) in self.children.iter().enumerate() {
464-
if i != index && child.is_some() {
465-
if child.as_ref().unwrap().search(&word[1..], 1) {
466-
return true;
467-
}
468-
}
476+
fn insert(&mut self, key: String, val: i32) {
477+
let x = val - self.d.get(&key).unwrap_or(&0);
478+
self.d.insert(key.clone(), val);
479+
self.trie.insert(&key, x);
480+
}
481+
482+
fn sum(&self, prefix: String) -> i32 {
483+
self.trie.search(&prefix)
484+
}
485+
}
486+
```
487+
488+
#### C#
489+
490+
```cs
491+
public class Trie {
492+
private Trie[] children = new Trie[26];
493+
private int val;
494+
495+
public void Insert(string w, int x) {
496+
Trie node = this;
497+
for (int i = 0; i < w.Length; ++i) {
498+
int idx = w[i] - 'a';
499+
if (node.children[idx] == null) {
500+
node.children[idx] = new Trie();
469501
}
502+
node = node.children[idx];
503+
node.val += x;
470504
}
505+
}
471506

472-
false
507+
public int Search(string w) {
508+
Trie node = this;
509+
for (int i = 0; i < w.Length; ++i) {
510+
int idx = w[i] - 'a';
511+
if (node.children[idx] == null) {
512+
return 0;
513+
}
514+
node = node.children[idx];
515+
}
516+
return node.val;
473517
}
474518
}
475519

476-
struct MagicDictionary {
477-
trie: Trie,
478-
}
520+
public class MapSum {
521+
private Dictionary<string, int> d = new Dictionary<string, int>();
522+
private Trie trie = new Trie();
479523

480-
/**
481-
* `&self` means the method takes an immutable reference.
482-
* If you need a mutable reference, change it to `&mut self` instead.
483-
*/
484-
impl MagicDictionary {
485-
fn new() -> Self {
486-
MagicDictionary { trie: Trie::new() }
524+
public MapSum() {
487525
}
488526

489-
fn build_dict(&mut self, dictionary: Vec<String>) {
490-
for word in dictionary {
491-
self.trie.insert(&word);
492-
}
527+
public void Insert(string key, int val) {
528+
int x = val - (d.ContainsKey(key) ? d[key] : 0);
529+
d[key] = val;
530+
trie.Insert(key, x);
493531
}
494532

495-
fn search(&self, search_word: String) -> bool {
496-
self.trie.search(&search_word, 0)
533+
public int Sum(string prefix) {
534+
return trie.Search(prefix);
497535
}
498536
}
537+
538+
/**
539+
* Your MapSum object will be instantiated and called as such:
540+
* MapSum obj = new MapSum();
541+
* obj.Insert(key,val);
542+
* int param_2 = obj.Sum(prefix);
543+
*/
499544
```
500545

501546
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
public class Trie {
2+
private Trie[] children = new Trie[26];
3+
private int val;
4+
5+
public void Insert(string w, int x) {
6+
Trie node = this;
7+
for (int i = 0; i < w.Length; ++i) {
8+
int idx = w[i] - 'a';
9+
if (node.children[idx] == null) {
10+
node.children[idx] = new Trie();
11+
}
12+
node = node.children[idx];
13+
node.val += x;
14+
}
15+
}
16+
17+
public int Search(string w) {
18+
Trie node = this;
19+
for (int i = 0; i < w.Length; ++i) {
20+
int idx = w[i] - 'a';
21+
if (node.children[idx] == null) {
22+
return 0;
23+
}
24+
node = node.children[idx];
25+
}
26+
return node.val;
27+
}
28+
}
29+
30+
public class MapSum {
31+
private Dictionary<string, int> d = new Dictionary<string, int>();
32+
private Trie trie = new Trie();
33+
34+
public MapSum() {
35+
}
36+
37+
public void Insert(string key, int val) {
38+
int x = val - (d.ContainsKey(key) ? d[key] : 0);
39+
d[key] = val;
40+
trie.Insert(key, x);
41+
}
42+
43+
public int Sum(string prefix) {
44+
return trie.Search(prefix);
45+
}
46+
}
47+
48+
/**
49+
* Your MapSum object will be instantiated and called as such:
50+
* MapSum obj = new MapSum();
51+
* obj.Insert(key,val);
52+
* int param_2 = obj.Sum(prefix);
53+
*/

0 commit comments

Comments
 (0)