Skip to content

Commit 46949e3

Browse files
committed
7-24-2020
1 parent b22c506 commit 46949e3

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/_0125_valid_palindrome.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn is_palindrome(s: String) -> bool {
5+
let s_chars: Vec<char> = s
6+
.chars()
7+
.filter(|c| c.is_ascii_alphanumeric())
8+
.map(|c| c.to_ascii_lowercase())
9+
.collect();
10+
let compare_1: String = s_chars.iter().collect();
11+
let compare_2: String = s_chars.iter().rev().collect();
12+
compare_1 == compare_2
13+
}
14+
}
15+
16+
#[test]
17+
fn test() {
18+
assert_eq!(
19+
Solution::is_palindrome("A man, a plan, a canal: Panama".to_string()),
20+
true
21+
);
22+
assert_eq!(Solution::is_palindrome("race a car".to_string()), false);
23+
}

src/_0763_partition_labels.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
struct Solution;
2+
3+
impl Solution {
4+
pub fn partition_labels(s: String) -> Vec<i32> {
5+
let mut h_occur_last: Vec<usize> = vec![0; 26];
6+
for (i, v) in s.chars().enumerate() {
7+
h_occur_last[v as usize - 'a' as usize] = i;
8+
}
9+
10+
let mut h_anchor: usize = 0;
11+
let mut h_block_last: usize = 0;
12+
let mut result: Vec<i32> = vec![];
13+
for (i, v) in s.chars().enumerate() {
14+
h_block_last = h_block_last.max(h_occur_last[v as usize - 'a' as usize]);
15+
if i == h_block_last {
16+
result.push((i - h_anchor + 1) as i32);
17+
h_anchor = i + 1;
18+
}
19+
}
20+
result
21+
}
22+
}
23+
24+
#[test]
25+
fn test() {
26+
assert_eq!(
27+
Solution::partition_labels("ababcbacadefegdehijhklij".to_string()),
28+
vec![9, 7, 8]
29+
);
30+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ mod _0088_merge_sorted_array;
5252
//
5353
mod _0121_best_time_to_buy_and_sell_stock;
5454
//
55+
mod _0125_valid_palindrome;
56+
//
5557
mod _0127_word_ladder;
5658
//
5759
mod _0136_single_number;
@@ -128,6 +130,8 @@ mod _0724_find_pivot_index;
128130
//
129131
mod _0735_asteroid_collision;
130132
//
133+
mod _0763_partition_labels;
134+
//
131135
mod _0771_jewels_and_stones;
132136
//
133137
mod _0796_rotate_string;

0 commit comments

Comments
 (0)