Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,33 @@ function countPalindromicSubsequence(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_palindromic_subsequence(s: String) -> i32 {
let s_bytes = s.as_bytes();
let mut ans = 0;
for c in b'a'..=b'z' {
if let (Some(l), Some(r)) = (
s_bytes.iter().position(|&ch| ch == c),
s_bytes.iter().rposition(|&ch| ch == c),
) {
let mut mask = 0u32;
for i in (l + 1)..r {
let j = (s_bytes[i] - b'a') as u32;
if (mask >> j & 1) == 0 {
mask |= 1 << j;
ans += 1;
}
}
}
}
ans
}
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,33 @@ function countPalindromicSubsequence(s: string): number {
}
```

#### Rust

```rust
impl Solution {
pub fn count_palindromic_subsequence(s: String) -> i32 {
let s_bytes = s.as_bytes();
let mut ans = 0;
for c in b'a'..=b'z' {
if let (Some(l), Some(r)) = (
s_bytes.iter().position(|&ch| ch == c),
s_bytes.iter().rposition(|&ch| ch == c),
) {
let mut mask = 0u32;
for i in (l + 1)..r {
let j = (s_bytes[i] - b'a') as u32;
if (mask >> j & 1) == 0 {
mask |= 1 << j;
ans += 1;
}
}
}
}
ans
}
}
```

#### JavaScript

```js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn count_palindromic_subsequence(s: String) -> i32 {
let s_bytes = s.as_bytes();
let mut ans = 0;
for c in b'a'..=b'z' {
if let (Some(l), Some(r)) = (
s_bytes.iter().position(|&ch| ch == c),
s_bytes.iter().rposition(|&ch| ch == c),
) {
let mut mask = 0u32;
for i in (l + 1)..r {
let j = (s_bytes[i] - b'a') as u32;
if (mask >> j & 1) == 0 {
mask |= 1 << j;
ans += 1;
}
}
}
}
ans
}
}