2605. Form Smallest Number From Two Digit Arrays
This commit is contained in:
parent
7e584cf405
commit
f522beca93
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "find-longest-special-substring-that-occurs-thrice-ii"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
|
@ -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.
|
||||
|
|
@ -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<usize> = vec![0; s.len()];
|
||||
let mut thrice_max_len: Vec<Vec<usize>> = 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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
//! 2982. Find Longest Special Substring That Occurs Thrice II
|
||||
|
||||
mod longest_special_substring;
|
||||
|
||||
fn main() {
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "form-smallest-number-from-two-digit-arrays"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
|
@ -0,0 +1,4 @@
|
|||
mod two_digits;
|
||||
|
||||
fn main() {
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
pub struct Solution;
|
||||
|
||||
impl Solution {
|
||||
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue