20240428 Finished

This commit is contained in:
jackfiled 2024-04-28 11:05:24 +08:00
parent 3ba7be1c29
commit e922f1fc4a
2 changed files with 87 additions and 1 deletions

View File

@ -114,4 +114,5 @@ mod p68_text_justification;
mod p125_valid_palindrome;
mod p392_is_subsequence;
mod p167_two_sum_ii_input_array_is_sorted;
mod p209_minimum_size_subarray_sum;
mod p209_minimum_size_subarray_sum;
mod p30_substring_with_concatenation_of_all_words;

View File

@ -0,0 +1,85 @@
/**
* [30] Substring with Concatenation of All Words
*/
pub struct Solution {}
// submission codes start here
use std::collections::HashMap;
impl Solution {
pub fn find_substring(s: String, words: Vec<String>) -> Vec<i32> {
let mut result = Vec::new();
let m = words.len();
let n = words[0].len();
let ls = s.len();
for start in 0..n {
if start + m * n > ls {
break;
}
let mut differ = HashMap::new();
for i in 0..m {
let word = s[start + i * n..start + i * n + n].to_owned();
let entry = differ.entry(word).or_insert(0);
*entry += 1;
}
for word in (&words).to_owned() {
let entry = differ.entry(word.clone()).or_insert(0);
*entry -= 1;
if *entry == 0 {
differ.remove(&word);
}
}
// 滑动窗口开始滑动
for i in (start..ls - m * n + 1).step_by(n) {
if i != start {
// 新加入的word
let end_word = s[i + (m - 1) * n..i + m * n].to_owned();
let entry = differ.entry(end_word.clone()).or_insert(0);
*entry += 1;
if *entry == 0 {
differ.remove(&end_word);
}
// 滑动后被移除的word
let begin_word = s[i - n..i].to_owned();
let entry = differ.entry(begin_word.clone()).or_insert(0);
*entry -= 1;
if *entry == 0 {
differ.remove(&begin_word);
}
}
if differ.len() == 0 {
result.push(i as i32);
}
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_30() {
assert_eq!(vec![0,9], Solution::find_substring("barfoothefoobarman".to_owned(), vec![
"foo".to_owned(),
"bar".to_owned()
]))
}
}