File tree 3 files changed +136
-0
lines changed
solution/1300-1399/1316.Distinct Echo Substrings
3 files changed +136
-0
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,53 @@ public:
161
161
};
162
162
```
163
163
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
+
164
211
### ** Go**
165
212
166
213
``` go
Original file line number Diff line number Diff line change @@ -137,6 +137,53 @@ public:
137
137
};
138
138
```
139
139
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
+
140
187
### ** Go**
141
188
142
189
``` go
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments