LeetCode in Kotlin

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

class Solution {
    fun passwordStrength(password: String): Int {
        var strength = 0
        val present = BooleanArray(256)
        for (i in 0..<password.length) {
            present[password[i].code] = true
        }
        for (i in 97..122) {
            if (present[i]) {
                strength++
            }
        }
        for (i in 65..90) {
            if (present[i]) {
                strength += 2
            }
        }
        for (i in 48..57) {
            if (present[i]) {
                strength += 3
            }
        }
        if (present['!'.code]) {
            strength += 5
        }
        if (present['@'.code]) {
            strength += 5
        }
        if (present['#'.code]) {
            strength += 5
        }
        if (present['$'.code]) {
            strength += 5
        }
        return strength
    }
}