diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 0636831..254afe6 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -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; diff --git a/src/problem/p3083_existence_of_a_substring_in_a_string_and_its_reverse.rs b/src/problem/p3083_existence_of_a_substring_in_a_string_and_its_reverse.rs new file mode 100644 index 0000000..5ef09ae --- /dev/null +++ b/src/problem/p3083_existence_of_a_substring_in_a_string_and_its_reverse.rs @@ -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 = 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())); + } +}