LeetCode in Kotlin

2299. Strong Password Checker II

Easy

A password is said to be strong if it satisfies all the following criteria:

Given a string password, return true if it is a strong password. Otherwise, return false.

Example 1:

Input: password = “IloveLe3tcode!”

Output: true

Explanation: The password meets all the requirements. Therefore, we return true.

Example 2:

Input: password = “Me+You–IsMyDream”

Output: false

Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.

Example 3:

Input: password = “1aB!”

Output: false

Explanation: The password does not meet the length requirement. Therefore, we return false.

Constraints:

Solution

class Solution {
    fun strongPasswordCheckerII(password: String): Boolean {
        if (password.length < 8) {
            return false
        }
        var l = false
        var u = false
        var d = false
        var s = false
        val special = "!@#$%^&*()-+"
        var previous = '.'
        for (i in 0 until password.length) {
            val ch = password[i]
            if (ch == previous) {
                return false
            }
            previous = ch
            if (ch >= 'A' && ch <= 'Z') {
                u = true
            } else if (ch >= 'a' && ch <= 'z') {
                l = true
            } else if (ch >= '0' && ch <= '9') {
                d = true
            } else if (special.indexOf(ch) != -1) {
                s = true
            }
        }
        return l && u && d && s
    }
}