LeetCode 1023. Camelcase Matching

Problem Statement


impl Solution {
fn try_match(q: &String, p: &String) ->bool {
let (m, n, mut i, mut j) = (p.len(), q.len(), 0, 0);
while i < n {
if j == m && q.chars().nth(i).unwrap().is_uppercase() {
return false;
}
if (j == m || p.chars().nth(j).unwrap().is_uppercase()) && q.chars().nth(i).unwrap().is_lowercase() {
i += 1;
continue;
}
let qi = q.chars().nth(i).unwrap();
let pj = p.chars().nth(j).unwrap();
if pj.is_uppercase() && qi.is_uppercase() && pj != qi {
return false;
}
if pj.is_lowercase() && pj != qi {
i += 1;
continue;
}
i+= 1; j += 1;
}
return i == n && j == m;
}

pub fn camel_match(queries: Vec<String>, pattern: String) -> Vec<bool> {
let mut ans = Vec::new();
for q in &queries {
ans.push(Self::try_match(q, &pattern));
}
return ans;
}
}

LeetCode 1024. Video Stitching

Problem Statement

Greedy.


impl Solution {
pub fn video_stitching(clips: Vec<Vec<i32>>, t: i32) -> i32 {
let mut input = clips;
input.sort_by_key(|x| x[0]);
let (mut cur_end, mut max_end, mut ans, mut j) = (0, 0, 0, 0);
for i in 0..input.len() {
if i < j {
continue;
}
if input[i][0] > cur_end {
return -1;
}
max_end = cur_end;
j = i;
while j < input.len() {
if input[j][0] > cur_end {
break;
}
max_end = std::cmp::max(max_end, input[j][1]);
j += 1;
}
ans += 1;
cur_end = max_end;
if max_end >= t {
return ans;
}
}

return -1;
}
}