Skip to content

Commit ec27aeb

Browse files
authored
fix: update solutions to lc problem: No.0677 (#4276)
1 parent 479a4b2 commit ec27aeb

File tree

7 files changed

+441
-199
lines changed

7 files changed

+441
-199
lines changed

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

+43-84
Original file line numberDiff line numberDiff line change
@@ -431,124 +431,83 @@ class MagicDictionary {
431431

432432
```rust
433433
struct Trie {
434-
children: Vec<Option<Box<Trie>>>,
435-
val: i32,
434+
children: [Option<Box<Trie>>; 26],
435+
is_end: bool,
436436
}
437437

438438
impl Trie {
439439
fn new() -> Self {
440440
Trie {
441-
children: (0..26).map(|_| None).collect(),
442-
val: 0,
441+
children: Default::default(),
442+
is_end: false,
443443
}
444444
}
445445

446-
fn insert(&mut self, w: &str, x: i32) {
446+
fn insert(&mut self, w: &str) {
447447
let mut node = self;
448448
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()));
449+
let i = (c as usize) - ('a' as usize);
450+
if node.children[i].is_none() {
451+
node.children[i] = Some(Box::new(Trie::new()));
452452
}
453-
node = node.children[idx].as_mut().unwrap();
454-
node.val += x;
453+
node = node.children[i].as_mut().unwrap();
455454
}
455+
node.is_end = true;
456456
}
457457

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();
466-
}
467-
node.val
458+
fn search(&self, w: &str) -> bool {
459+
self.dfs(w, 0, 0)
468460
}
469-
}
470461

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(),
462+
fn dfs(&self, w: &str, i: usize, diff: usize) -> bool {
463+
if i == w.len() {
464+
return diff == 1 && self.is_end;
481465
}
482-
}
483-
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-
}
489466

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;
467+
let j = (w.chars().nth(i).unwrap() as usize) - ('a' as usize);
502468

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();
469+
if let Some(child) = &self.children[j] {
470+
if child.dfs(w, i + 1, diff) {
471+
return true;
509472
}
510-
node = node.children[idx];
511-
node.val += x;
512473
}
513-
}
514474

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;
475+
if diff == 0 {
476+
for k in 0..26 {
477+
if k != j {
478+
if let Some(child) = &self.children[k] {
479+
if child.dfs(w, i + 1, 1) {
480+
return true;
481+
}
482+
}
483+
}
521484
}
522-
node = node.children[idx];
523485
}
524-
return node.val;
486+
false
525487
}
526488
}
527489

528-
public class MapSum {
529-
private Dictionary<string, int> d = new Dictionary<string, int>();
530-
private Trie trie = new Trie();
490+
struct MagicDictionary {
491+
trie: Trie,
492+
}
531493

532-
public MapSum() {
494+
impl MagicDictionary {
495+
fn new() -> Self {
496+
MagicDictionary {
497+
trie: Trie::new(),
498+
}
533499
}
534500

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);
501+
fn build_dict(&mut self, dictionary: Vec<String>) {
502+
for w in dictionary {
503+
self.trie.insert(&w);
504+
}
539505
}
540506

541-
public int Sum(string prefix) {
542-
return trie.Search(prefix);
507+
fn search(&self, search_word: String) -> bool {
508+
self.trie.search(&search_word)
543509
}
544510
}
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-
*/
552511
```
553512

554513
<!-- tabs:end -->

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

+43-84
Original file line numberDiff line numberDiff line change
@@ -423,124 +423,83 @@ class MagicDictionary {
423423

424424
```rust
425425
struct Trie {
426-
children: Vec<Option<Box<Trie>>>,
427-
val: i32,
426+
children: [Option<Box<Trie>>; 26],
427+
is_end: bool,
428428
}
429429

430430
impl Trie {
431431
fn new() -> Self {
432432
Trie {
433-
children: (0..26).map(|_| None).collect(),
434-
val: 0,
433+
children: Default::default(),
434+
is_end: false,
435435
}
436436
}
437437

438-
fn insert(&mut self, w: &str, x: i32) {
438+
fn insert(&mut self, w: &str) {
439439
let mut node = self;
440440
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()));
441+
let i = (c as usize) - ('a' as usize);
442+
if node.children[i].is_none() {
443+
node.children[i] = Some(Box::new(Trie::new()));
444444
}
445-
node = node.children[idx].as_mut().unwrap();
446-
node.val += x;
445+
node = node.children[i].as_mut().unwrap();
447446
}
447+
node.is_end = true;
448448
}
449449

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();
458-
}
459-
node.val
450+
fn search(&self, w: &str) -> bool {
451+
self.dfs(w, 0, 0)
460452
}
461-
}
462453

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(),
454+
fn dfs(&self, w: &str, i: usize, diff: usize) -> bool {
455+
if i == w.len() {
456+
return diff == 1 && self.is_end;
473457
}
474-
}
475-
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-
}
481458

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;
459+
let j = (w.chars().nth(i).unwrap() as usize) - ('a' as usize);
494460

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();
461+
if let Some(child) = &self.children[j] {
462+
if child.dfs(w, i + 1, diff) {
463+
return true;
501464
}
502-
node = node.children[idx];
503-
node.val += x;
504465
}
505-
}
506466

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;
467+
if diff == 0 {
468+
for k in 0..26 {
469+
if k != j {
470+
if let Some(child) = &self.children[k] {
471+
if child.dfs(w, i + 1, 1) {
472+
return true;
473+
}
474+
}
475+
}
513476
}
514-
node = node.children[idx];
515477
}
516-
return node.val;
478+
false
517479
}
518480
}
519481

520-
public class MapSum {
521-
private Dictionary<string, int> d = new Dictionary<string, int>();
522-
private Trie trie = new Trie();
482+
struct MagicDictionary {
483+
trie: Trie,
484+
}
523485

524-
public MapSum() {
486+
impl MagicDictionary {
487+
fn new() -> Self {
488+
MagicDictionary {
489+
trie: Trie::new(),
490+
}
525491
}
526492

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);
493+
fn build_dict(&mut self, dictionary: Vec<String>) {
494+
for w in dictionary {
495+
self.trie.insert(&w);
496+
}
531497
}
532498

533-
public int Sum(string prefix) {
534-
return trie.Search(prefix);
499+
fn search(&self, search_word: String) -> bool {
500+
self.trie.search(&search_word)
535501
}
536502
}
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-
*/
544503
```
545504

546505
<!-- tabs:end -->

0 commit comments

Comments
 (0)