20241226 finished.

This commit is contained in:
jackfiled 2024-12-26 11:58:56 +08:00
parent 24b94dfde9
commit 0bc27473df
2 changed files with 47 additions and 0 deletions

View File

@ -392,3 +392,5 @@ mod p855_exam_room;
mod p1705_maximum_number_of_eaten_apples;
mod p3218_minimum_cost_for_cutting_cake_i;
mod p3083_existence_of_a_substring_in_a_string_and_its_reverse;

View File

@ -0,0 +1,45 @@
/**
* [3083] Existence of a Substring in a String and Its Reverse
*/
pub struct Solution {}
// submission codes start here
use std::collections::{HashMap, HashSet};
impl Solution {
pub fn is_substring_present(s: String) -> bool {
let s: Vec<char> = s.chars().collect();
let n = s.len();
let mut map = HashMap::new();
for i in (1..n).rev() {
let entry = map.entry(s[i]).or_insert(HashSet::new());
entry.insert(s[i - 1]);
}
for i in 0..n - 1 {
if let Some(set) = map.get(&s[i]) {
if set.contains(&s[i + 1]) {
return true;
}
}
}
false
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3083() {
assert!(Solution::is_substring_present("leetcode".to_owned()));
assert!(Solution::is_substring_present("abcba".to_owned()));
assert!(!Solution::is_substring_present("abcd".to_owned()));
}
}