20250314 finished.

This commit is contained in:
jackfiled 2025-03-14 11:23:06 +08:00
parent 9f09445dab
commit 3c2c63f4ea
2 changed files with 29 additions and 0 deletions

View File

@ -536,3 +536,5 @@ mod p2012_sum_of_beauty_in_the_array;
mod p3305_count_of_substrings_containing_every_vowel_and_k_consonants_i;
mod p3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii;
mod p3340_check_balanced_string;

View File

@ -0,0 +1,27 @@
/**
* [3340] Check Balanced String
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn is_balanced(num: String) -> bool {
let num: Vec<i32> = num.bytes().map(|x| (x - b'0') as i32).collect();
num.iter().step_by(2).sum::<i32>() == num.iter().skip(1).step_by(2).sum::<i32>()
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3340() {
assert!(!Solution::is_balanced("1234".to_owned()));
assert!(Solution::is_balanced("24123".to_owned()));
}
}