Strong Password

  • + 0 comments

    My rust solution:

    const SPECIAL_CHARS: &'static str = "!@#$%^&*()-+";
    fn minimumNumber(n: i32, password: &str) -> i32 {
        // Return the minimum number of characters to make the password strong
        let missing_char = if n < 6 { 6 - n } else { 0 };
        let mut criteria = [0; 4];
        
        for c in password.chars() {
            if SPECIAL_CHARS.contains(c) {
                criteria[0] += 1;
            } else if c.is_digit(10) {
                criteria[1] += 1;
            } else if c.is_lowercase() {
                criteria[2] += 1;
            } else if c.is_uppercase() { 
                criteria[3] += 1; 
            }
        }
        
        let missing_criteria = criteria.iter().map(|&x| if x > 0 { 0 } else { 1 }).sum::<i32>();
        if missing_char >= missing_criteria { missing_char } else { missing_criteria }     
    }