#Reto #182 - Minimum String Length After Removing Substrings

1 messages · Page 1 of 1 (latest)

lyric yarrowBOT
#

You are given a string s consisting only of uppercase English letters.

You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings "AB" or "CD" from s.

Return the minimum possible length of the resulting string that you can obtain.

Note that the string concatenates after removing the substring and could produce new "AB" or "CD" substrings.

Example 1:

Input: s = "ABFCACDB"Output: 2Explanation: We can do the following operations:- Remove the substring "ABFCACDB", so s = "FCACDB".- Remove the substring "FCACDB", so s = "FCAB".- Remove the substring "FCAB", so s = "FC".So the resulting length of the string is 2.It can be shown that it is the minimum length that we can obtain.

Example 2:

Input: s = "ACBBD"Output: 5Explanation: We cannot do any operations on the string so the length remains the same.

Enlace:
https://leetcode.com/problems/minimum-string-length-after-removing-substrings/

Casos de Prueba

  • "ABFCACDB"
  • "ACBBD"
struct Solution;

impl Solution {
    pub fn min_length(s: String) -> i32 {
        
    }
}
oak kayak
#

||
impl Solution {
pub fn min_length(s: String) -> i32 {
let mut s = s;
let ab = "AB".to_string();
let cd = "CD".to_string();
loop {
if s.contains(&ab) {
s = s.replace(&ab, "");
} else if s.contains(&cd) {
s = s.replace(&cd, "");
} else {
break;
}
}
s.len() as i32
}
}
||

wide swan
#

||```rs
struct Solution;

impl Solution {
pub fn min_length(s: String) -> i32 {
let mut s = s;
loop {
if !s.contains("AB") & !s.contains("CD") {
break;
}
s = s.replace("AB", "");
s = s.replace("CD", "");
}

    return s.len() as i32
}

}

#

si hay identacion mediocre es porque lo hice en celular