Skip to content

feat: Add rust implementation for NO.1316 #1163

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

Merged
merged 1 commit into from
Jul 7, 2023
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
47 changes: 47 additions & 0 deletions solution/1300-1399/1316.Distinct Echo Substrings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,53 @@ public:
};
```

### **Rust**

```rust
use std::collections::HashSet;

const BASE: u64 = 131;

impl Solution {
#[allow(dead_code)]
pub fn distinct_echo_substrings(text: String) -> i32 {
let n = text.len();
let mut vis: HashSet<u64> = HashSet::new();
let mut base_vec: Vec<u64> = vec![1; n + 1];
let mut hash_vec: Vec<u64> = vec![0; n + 1];

// Initialize the base vector & hash vector
for i in 0..n {
let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64;
// Update base vector
base_vec[i + 1] = base_vec[i] * BASE;
// Update hash vector
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
}

// Traverse the text to find the result pair, using rolling hash
for i in 0..n - 1 {
for j in i + 1..n {
// Prevent overflow
let k = i + (j - i) / 2;
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
if left == right {
vis.insert(left);
}
}
}

vis.len() as i32
}

#[allow(dead_code)]
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
}
}
```

### **Go**

```go
Expand Down
47 changes: 47 additions & 0 deletions solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,53 @@ public:
};
```

### **Rust**

```rust
use std::collections::HashSet;

const BASE: u64 = 131;

impl Solution {
#[allow(dead_code)]
pub fn distinct_echo_substrings(text: String) -> i32 {
let n = text.len();
let mut vis: HashSet<u64> = HashSet::new();
let mut base_vec: Vec<u64> = vec![1; n + 1];
let mut hash_vec: Vec<u64> = vec![0; n + 1];

// Initialize the base vector & hash vector
for i in 0..n {
let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64;
// Update base vector
base_vec[i + 1] = base_vec[i] * BASE;
// Update hash vector
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
}

// Traverse the text to find the result pair, using rolling hash
for i in 0..n - 1 {
for j in i + 1..n {
// Prevent overflow
let k = i + (j - i) / 2;
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
if left == right {
vis.insert(left);
}
}
}

vis.len() as i32
}

#[allow(dead_code)]
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
}
}
```

### **Go**

```go
Expand Down
42 changes: 42 additions & 0 deletions solution/1300-1399/1316.Distinct Echo Substrings/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::collections::HashSet;

const BASE: u64 = 131;

impl Solution {
#[allow(dead_code)]
pub fn distinct_echo_substrings(text: String) -> i32 {
let n = text.len();
let mut vis: HashSet<u64> = HashSet::new();
let mut base_vec: Vec<u64> = vec![1; n + 1];
let mut hash_vec: Vec<u64> = vec![0; n + 1];

// Initialize the base vector & hash vector
for i in 0..n {
let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64;
// Update base vector
base_vec[i + 1] = base_vec[i] * BASE;
// Update hash vector
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
}

// Traverse the text to find the result pair, using rolling hash
for i in 0..n - 1 {
for j in i + 1..n {
// Prevent overflow
let k = i + (j - i) / 2;
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
if left == right {
vis.insert(left);
}
}
}

vis.len() as i32
}

#[allow(dead_code)]
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
}
}