8000 count substrings · jhideki/rust_leetcode@18fbd56 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18fbd56

Browse files
committed
count substrings
1 parent 8c7356d commit 18fbd56

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
mod longest_palindromic;
1+
mod palindromic_substrings;
22
fn main() {}

src/palindromic_substrings.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
pub struct Solution {}
2+
impl Solution {
3+
pub fn count_substrings(s: String) -> i32 {
4+
let s = s.as_bytes();
5+
let mut res = 0;
6+
for i in 0..s.len() {
7+
let (l, mut r) = (i as i32, i as i32);
8+
res += Self::count(s, l, r);
9+
r += 1;
10+
res += Self::count(s, l, r);
11+
}
12+
res
13+
}
14+
fn count(s: &[u8], mut l: i32, mut r: i32) -> i32 {
15+
let mut res = 0;
16+
while l >= 0 && r < s.len() as i32 && s[l as usize] == s[r as usize] {
17+
res += 1;
18+
l -= 1;
19+
r += 1;
20+
}
21+
res
22+
}
23+
}

0 commit comments

Comments
 (0)
0