Skip to content

Commit 6315674

Browse files
authored
feat: add rust solution to lc problem: No.1316 (doocs#1163)
1 parent afdb624 commit 6315674

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

Diff for: solution/1300-1399/1316.Distinct Echo Substrings/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,53 @@ public:
161161
};
162162
```
163163

164+
### **Rust**
165+
166+
```rust
167+
use std::collections::HashSet;
168+
169+
const BASE: u64 = 131;
170+
171+
impl Solution {
172+
#[allow(dead_code)]
173+
pub fn distinct_echo_substrings(text: String) -> i32 {
174+
let n = text.len();
175+
let mut vis: HashSet<u64> = HashSet::new();
176+
let mut base_vec: Vec<u64> = vec![1; n + 1];
177+
let mut hash_vec: Vec<u64> = vec![0; n + 1];
178+
179+
// Initialize the base vector & hash vector
180+
for i in 0..n {
181+
let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64;
182+
// Update base vector
183+
base_vec[i + 1] = base_vec[i] * BASE;
184+
// Update hash vector
185+
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
186+
}
187+
188+
// Traverse the text to find the result pair, using rolling hash
189+
for i in 0..n - 1 {
190+
for j in i + 1..n {
191+
// Prevent overflow
192+
let k = i + (j - i) / 2;
193+
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
194+
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
195+
if left == right {
196+
vis.insert(left);
197+
}
198+
}
199+
}
200+
201+
vis.len() as i32
202+
}
203+
204+
#[allow(dead_code)]
205+
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
206+
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
207+
}
208+
}
209+
```
210+
164211
### **Go**
165212

166213
```go

Diff for: solution/1300-1399/1316.Distinct Echo Substrings/README_EN.md

+47
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,53 @@ public:
137137
};
138138
```
139139

140+
### **Rust**
141+
142+
```rust
143+
use std::collections::HashSet;
144+
145+
const BASE: u64 = 131;
146+
147+
impl Solution {
148+
#[allow(dead_code)]
149+
pub fn distinct_echo_substrings(text: String) -> i32 {
150+
let n = text.len();
151+
let mut vis: HashSet<u64> = HashSet::new();
152+
let mut base_vec: Vec<u64> = vec![1; n + 1];
153+
let mut hash_vec: Vec<u64> = vec![0; n + 1];
154+
155+
// Initialize the base vector & hash vector
156+
for i in 0..n {
157+
let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64;
158+
// Update base vector
159+
base_vec[i + 1] = base_vec[i] * BASE;
160+
// Update hash vector
161+
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
162+
}
163+
164+
// Traverse the text to find the result pair, using rolling hash
165+
for i in 0..n - 1 {
166+
for j in i + 1..n {
167+
// Prevent overflow
168+
let k = i + (j - i) / 2;
169+
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
170+
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
171+
if left == right {
172+
vis.insert(left);
173+
}
174+
}
175+
}
176+
177+
vis.len() as i32
178+
}
179+
180+
#[allow(dead_code)]
181+
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
182+
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
183+
}
184+
}
185+
```
186+
140187
### **Go**
141188

142189
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use std::collections::HashSet;
2+
3+
const BASE: u64 = 131;
4+
5+
impl Solution {
6+
#[allow(dead_code)]
7+
pub fn distinct_echo_substrings(text: String) -> i32 {
8+
let n = text.len();
9+
let mut vis: HashSet<u64> = HashSet::new();
10+
let mut base_vec: Vec<u64> = vec![1; n + 1];
11+
let mut hash_vec: Vec<u64> = vec![0; n + 1];
12+
13+
// Initialize the base vector & hash vector
14+
for i in 0..n {
15+
let cur_char = (text.chars().nth(i).unwrap() as u8 - 'a' as u8 + 1) as u64;
16+
// Update base vector
17+
base_vec[i + 1] = base_vec[i] * BASE;
18+
// Update hash vector
19+
hash_vec[i + 1] = hash_vec[i] * BASE + cur_char;
20+
}
21+
22+
// Traverse the text to find the result pair, using rolling hash
23+
for i in 0..n - 1 {
24+
for j in i + 1..n {
25+
// Prevent overflow
26+
let k = i + (j - i) / 2;
27+
let left = Self::get_hash(i + 1, k + 1, &base_vec, &hash_vec);
28+
let right = Self::get_hash(k + 2, j + 1, &base_vec, &hash_vec);
29+
if left == right {
30+
vis.insert(left);
31+
}
32+
}
33+
}
34+
35+
vis.len() as i32
36+
}
37+
38+
#[allow(dead_code)]
39+
fn get_hash(start: usize, end: usize, base_vec: &Vec<u64>, hash_vec: &Vec<u64>) -> u64 {
40+
hash_vec[end] - hash_vec[start - 1] * base_vec[end - start + 1]
41+
}
42+
}

0 commit comments

Comments
 (0)