LeetCode-in-Java

3941. Password Strength

Medium

You are given a string password.

The strength of the password is calculated based on the following rules:

Each character contributes at most once, even if it appears multiple times.

Return an integer denoting the strength of the password.

Example 1:

Input: password = “aA1!”

Output: 11

Explanation:

Example 2:

Input: password = “bbB11#”

Output: 11

Explanation:

Constraints:

Solution

public class Solution {
    public int passwordStrength(String password) {
        int strength = 0;
        boolean[] present = new boolean[256];
        for (int i = 0; i < password.length(); i++) {
            present[password.charAt(i)] = true;
        }
        for (int i = 97; i <= 122; i++) {
            if (present[i]) {
                strength++;
            }
        }
        for (int i = 65; i <= 90; i++) {
            if (present[i]) {
                strength += 2;
            }
        }
        for (int i = 48; i <= 57; i++) {
            if (present[i]) {
                strength += 3;
            }
        }
        if (present['!']) {
            strength += 5;
        }
        if (present['@']) {
            strength += 5;
        }
        if (present['#']) {
            strength += 5;
        }
        if (present['$']) {
            strength += 5;
        }
        return strength;
    }
}