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

密码验证合格程序

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#
全部评论

相关推荐

05-09 14:45
门头沟学院 Java
点赞 评论 收藏
分享
牛客773130651号:巨佬,简历模板换成上下的,左右的很烦,hr看着不爽。。。科大随便乱杀,建议能保研就保研,不行也得考一下 ,985硕去干算法,比开发强多了。开发许多双非都能搞,学历优势用不上,算法有门槛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务