题解 | #密码验证合格程序#

密码验证合格程序

https://www.nowcoder.com/practice/184edec193864f0985ad2684fbc86841


import java.util.Scanner

fun main(args: Array<String>) {
    val sc = Scanner(System.`in`)
    while (sc.hasNext()) {
        val str = sc.next()
        if (str.length <= 8) {
            println("NG")
            continue
        }
        if (getMatch(str)) {
            println("NG")
            continue
        }
        if (getString(str, 0, 3)) {
            println("NG")
            continue
        }
        println("OK")
    }
}

// 校验是否有重复子串
tailrec fun getString(str: String, l: Int, r: Int): Boolean {
    return if (r >= str.length) {
        false
    } else if (str.substring(r).contains(str.substring(l, r))) {
        true
    } else {
        getString(str, l + 1, r + 1)
    }
}

// 检查是否满足正则
fun getMatch(str: String): Boolean {
    var count = 0

    if (str.count { it.isUpperCase() } > 0) {
        count++
    }
    if (str.count { it.isLowerCase() } > 0) {
        count++
    }
    if (str.count { it.isDigit() } > 0) {
        count++
    }
    if (str.count { !(it.isDigit() && it.isLowerCase() && it.isUpperCase()) } > 0) {
        count++
    }
    return count < 3
}

#kotlin#
全部评论

相关推荐

被普调的六边形战士很高大:项目经历貌似和专业或者求职方向没大关系?
点赞 评论 收藏
分享
人生一梦:24年我投暑期实习,它以我不是女的为理由拒绝了我查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务