3090. 每个字符最多出现两次的最长子字符串

This commit is contained in:
2026-08-14 20:20:45 +08:00
parent 7b7fdf87c3
commit d19b5d9ca8
+13 -24
View File
@@ -6,36 +6,25 @@ mod tree;
struct Solution {}
impl Solution {
pub fn decode_string(s: String) -> String {
let mut st = vec![];
let mut local = "".to_string();
let mut cnt = 0;
// let mut layer = 0;
for c in s.chars() {
if c.is_ascii_digit() {
cnt = cnt * 10 + c.to_digit(10).unwrap() as usize;
continue;
pub fn maximum_length_substring(s: String) -> i32 {
let s = s.bytes().map(|x| x as usize).collect::<Vec<_>>();
let mut arr = vec![0; 'z' as usize + 1];
let mut l = 0;
let mut ans = 0;
for r in 0..s.len() {
arr[s[r]] += 1;
while arr[s[r]] > 2 {
arr[s[l]] -= 1;
l += 1;
}
if c == '[' {
st.push((local,cnt));
cnt = 0;
local = "".to_string();
continue;
}
if c == ']' {
let (mut str, cnt) = st.pop().unwrap();
str += &local.repeat( cnt);
local = str;
continue;
}
local.push(c);
ans = ans.max(r - l + 1);
}
local
ans as i32
}
}
fn main() {
println!(
"{:?}",
Solution::decode_string("3[a2[c]]".to_string())
Solution::maximum_length_substring("bcbbbcba".to_string())
);
}