Skip to content

Commit 86225ea

Browse files
authored
feat: add rust solution to lc problem: No.1930 (#4854)
1 parent aa8b52a commit 86225ea

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,33 @@ function countPalindromicSubsequence(s: string): number {
191191
}
192192
```
193193

194+
#### Rust
195+
196+
```rust
197+
impl Solution {
198+
pub fn count_palindromic_subsequence(s: String) -> i32 {
199+
let s_bytes = s.as_bytes();
200+
let mut ans = 0;
201+
for c in b'a'..=b'z' {
202+
if let (Some(l), Some(r)) = (
203+
s_bytes.iter().position(|&ch| ch == c),
204+
s_bytes.iter().rposition(|&ch| ch == c),
205+
) {
206+
let mut mask = 0u32;
207+
for i in (l + 1)..r {
208+
let j = (s_bytes[i] - b'a') as u32;
209+
if (mask >> j & 1) == 0 {
210+
mask |= 1 << j;
211+
ans += 1;
212+
}
213+
}
214+
}
215+
}
216+
ans
217+
}
218+
}
219+
```
220+
194221
#### JavaScript
195222

196223
```js

solution/1900-1999/1930.Unique Length-3 Palindromic Subsequences/README_EN.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,33 @@ function countPalindromicSubsequence(s: string): number {
189189
}
190190
```
191191

192+
#### Rust
193+
194+
```rust
195+
impl Solution {
196+
pub fn count_palindromic_subsequence(s: String) -> i32 {
197+
let s_bytes = s.as_bytes();
198+
let mut ans = 0;
199+
for c in b'a'..=b'z' {
200+
if let (Some(l), Some(r)) = (
201+
s_bytes.iter().position(|&ch| ch == c),
202+
s_bytes.iter().rposition(|&ch| ch == c),
203+
) {
204+
let mut mask = 0u32;
205+
for i in (l + 1)..r {
206+
let j = (s_bytes[i] - b'a') as u32;
207+
if (mask >> j & 1) == 0 {
208+
mask |= 1 << j;
209+
ans += 1;
210+
}
211+
}
212+
}
213+
}
214+
ans
215+
}
216+
}
217+
```
218+
192219
#### JavaScript
193220

194221
```js
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn count_palindromic_subsequence(s: String) -> i32 {
3+
let s_bytes = s.as_bytes();
4+
let mut ans = 0;
5+
for c in b'a'..=b'z' {
6+
if let (Some(l), Some(r)) = (
7+
s_bytes.iter().position(|&ch| ch == c),
8+
s_bytes.iter().rposition(|&ch| ch == c),
9+
) {
10+
let mut mask = 0u32;
11+
for i in (l + 1)..r {
12+
let j = (s_bytes[i] - b'a') as u32;
13+
if (mask >> j & 1) == 0 {
14+
mask |= 1 << j;
15+
ans += 1;
16+
}
17+
}
18+
}
19+
}
20+
ans
21+
}
22+
}

0 commit comments

Comments
 (0)