From f522beca938331b9bcd0a71ec021da4ef47e7b5c Mon Sep 17 00:00:00 2001 From: Samuel Ortion Date: Tue, 11 Mar 2025 09:22:20 +0100 Subject: [PATCH] 2605. Form Smallest Number From Two Digit Arrays --- .../Cargo.toml | 6 ++ .../README.md | 40 ++++++++++++ .../src/longest_special_substring.rs | 61 +++++++++++++++++++ .../src/main.rs | 7 +++ .../Cargo.toml | 6 ++ .../src/main.rs | 4 ++ .../src/two_digits.rs | 43 +++++++++++++ 7 files changed, 167 insertions(+) create mode 100644 rust/find-longest-special-substring-that-occurs-thrice-ii/Cargo.toml create mode 100644 rust/find-longest-special-substring-that-occurs-thrice-ii/README.md create mode 100644 rust/find-longest-special-substring-that-occurs-thrice-ii/src/longest_special_substring.rs create mode 100644 rust/find-longest-special-substring-that-occurs-thrice-ii/src/main.rs create mode 100644 rust/form-smallest-number-from-two-digit-arrays/Cargo.toml create mode 100644 rust/form-smallest-number-from-two-digit-arrays/src/main.rs create mode 100644 rust/form-smallest-number-from-two-digit-arrays/src/two_digits.rs diff --git a/rust/find-longest-special-substring-that-occurs-thrice-ii/Cargo.toml b/rust/find-longest-special-substring-that-occurs-thrice-ii/Cargo.toml new file mode 100644 index 0000000..588aa3c --- /dev/null +++ b/rust/find-longest-special-substring-that-occurs-thrice-ii/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "find-longest-special-substring-that-occurs-thrice-ii" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rust/find-longest-special-substring-that-occurs-thrice-ii/README.md b/rust/find-longest-special-substring-that-occurs-thrice-ii/README.md new file mode 100644 index 0000000..bd1f151 --- /dev/null +++ b/rust/find-longest-special-substring-that-occurs-thrice-ii/README.md @@ -0,0 +1,40 @@ +# 2982. Find Longest Special Substring That Occurs Thrice II + + +You are given a string s that consists of lowercase English letters. + +A string is called special if it is made up of only a single character. For example, the string "abc" is not special, whereas the strings "ddd", "zz", and "f" are special. + +Return the length of the longest special substring of s which occurs at least thrice, or -1 if no special substring occurs at least thrice. + +A substring is a contiguous non-empty sequence of characters within a string. + + + +Example 1: + +Input: s = "aaaa" +Output: 2 +Explanation: The longest special substring which occurs thrice is "aa": substrings "aaaa", "aaaa", and "aaaa". +It can be shown that the maximum length achievable is 2. + +Example 2: + +Input: s = "abcdef" +Output: -1 +Explanation: There exists no special substring which occurs at least thrice. Hence return -1. + +Example 3: + +Input: s = "abcaba" +Output: 1 +Explanation: The longest special substring which occurs thrice is "a": substrings "abcaba", "abcaba", and "abcaba". +It can be shown that the maximum length achievable is 1. + + + +Constraints: + + 3 <= s.length <= 5 * 105 + s consists of only lowercase English letters. + diff --git a/rust/find-longest-special-substring-that-occurs-thrice-ii/src/longest_special_substring.rs b/rust/find-longest-special-substring-that-occurs-thrice-ii/src/longest_special_substring.rs new file mode 100644 index 0000000..3e28867 --- /dev/null +++ b/rust/find-longest-special-substring-that-occurs-thrice-ii/src/longest_special_substring.rs @@ -0,0 +1,61 @@ +//! 2982. Find Longest Special Substring That Occurs Thrice II + +pub struct Solution; + +use std::collections::HashMap; + +impl Solution { + pub fn maximum_length(s: String) -> i32 { + let mut len: Vec = vec![0; s.len()]; + let mut thrice_max_len: Vec> = vec![vec![0; 3]; 26]; + let mut previous_letter = ' '; + for (index, letter) in s.chars().enumerate() { + if index > 0 && letter == previous_letter { + len[index] = len[index - 1] + 1; + } else { + len[index] = 1; + previous_letter = letter; + } + let letter_index = letter as usize - 'a' as usize; + if thrice_max_len[letter_index][0] < len[index] { + thrice_max_len[letter_index][0] = len[index]; + thrice_max_len[letter_index].sort(); + } + } + let mut current_max: usize = 0; + for thrice_len in thrice_max_len { + println!("{:?}", thrice_len); + if thrice_len[0] > 0 && current_max < thrice_len[0] { + current_max = thrice_len[0]; + } + } + if current_max > 0 { + return current_max as i32; + } else { + return -1; + } + } +} + +#[cfg(test)] +mod tests { + use crate::longest_special_substring::Solution; + + #[test] + fn test_1() { + let s = String::from("aaaa"); + assert_eq!(Solution::maximum_length(s), 2); + } + + #[test] + fn test_2() { + let s = String::from("abcdef"); + assert_eq!(Solution::maximum_length(s), -1); + } + + #[test] + fn test_3() { + let s = String::from("abcaba"); + assert_eq!(Solution::maximum_length(s), 1); + } +} diff --git a/rust/find-longest-special-substring-that-occurs-thrice-ii/src/main.rs b/rust/find-longest-special-substring-that-occurs-thrice-ii/src/main.rs new file mode 100644 index 0000000..3d0a8cd --- /dev/null +++ b/rust/find-longest-special-substring-that-occurs-thrice-ii/src/main.rs @@ -0,0 +1,7 @@ +//! 2982. Find Longest Special Substring That Occurs Thrice II + +mod longest_special_substring; + +fn main() { + +} diff --git a/rust/form-smallest-number-from-two-digit-arrays/Cargo.toml b/rust/form-smallest-number-from-two-digit-arrays/Cargo.toml new file mode 100644 index 0000000..1a85553 --- /dev/null +++ b/rust/form-smallest-number-from-two-digit-arrays/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "form-smallest-number-from-two-digit-arrays" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/rust/form-smallest-number-from-two-digit-arrays/src/main.rs b/rust/form-smallest-number-from-two-digit-arrays/src/main.rs new file mode 100644 index 0000000..6a5f510 --- /dev/null +++ b/rust/form-smallest-number-from-two-digit-arrays/src/main.rs @@ -0,0 +1,4 @@ +mod two_digits; + +fn main() { +} diff --git a/rust/form-smallest-number-from-two-digit-arrays/src/two_digits.rs b/rust/form-smallest-number-from-two-digit-arrays/src/two_digits.rs new file mode 100644 index 0000000..8d775d0 --- /dev/null +++ b/rust/form-smallest-number-from-two-digit-arrays/src/two_digits.rs @@ -0,0 +1,43 @@ +pub struct Solution; + +impl Solution { + pub fn min_number(nums1: Vec, nums2: Vec) -> i32 { + let mut min: i32 = nums1[0] * 10 + nums2[0]; + for i in 0..nums1.len() { + for j in 0..nums2.len() { + if nums1[i] == nums2[j] && nums1[i] < min { + min = nums1[i]; + } else { + let mut num = nums1[i] * 10 + nums2[j]; + if num < min { + min = num; + } + num = nums2[j] * 10 + nums1[i]; + if num < min { + min = num; + } + } + } + } + min + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_1() { + let nums1 = vec![4, 1, 3]; + let nums2 = vec![5,7]; + assert_eq!(Solution::min_number(nums1, nums2), 15); + } + + #[test] + fn test_2() { + let nums1 = vec![3, 5, 2, 6]; + let nums2 = vec![3, 1, 7]; + assert_eq!(Solution::min_number(nums1, nums2), 3); + } +}