LeetCode 题解工作台

强密码检验器

满足以下条件的密码被认为是强密码: 由至少 6 个,至多 20 个字符组成。 包含至少 一个小写 字母,至少 一个大写 字母,和至少 一个数字 。 不包含连续三个重复字符 (比如 "B aaa bb0" 是弱密码, 但是 "B aa b a 0" 是强密码)。 给你一个字符串 password ,返…

category

3

题型

code_blocks

3

代码语言

hub

3

相关题

当前训练重点

困难 · 贪心·invariant

bolt

答案摘要

class Solution: def strongPasswordChecker(self, password: str) -> int:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 贪心·invariant 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

满足以下条件的密码被认为是强密码:

  • 由至少 6 个,至多 20 个字符组成。
  • 包含至少 一个小写 字母,至少 一个大写 字母,和至少 一个数字
  • 不包含连续三个重复字符 (比如 "Baaabb0" 是弱密码, 但是 "Baaba0" 是强密码)。

给你一个字符串 password ,返回 password 修改到满足强密码条件需要的最少修改步数。如果 password 已经是强密码,则返回 0

在一步修改操作中,你可以:

  • 插入一个字符到 password
  • password 中删除一个字符,或
  • 用另一个字符来替换 password 中的某个字符。

 

示例 1:

输入:password = "a"
输出:5

示例 2:

输入:password = "aA1"
输出:3

示例 3:

输入:password = "1337C0d3"
输出:0

 

提示:

  • 1 <= password.length <= 50
  • password 由字母、数字、点 '.' 或者感叹号 '!' 组成
lightbulb

解题思路

方法一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution:
    def strongPasswordChecker(self, password: str) -> int:
        def countTypes(s):
            a = b = c = 0
            for ch in s:
                if ch.islower():
                    a = 1
                elif ch.isupper():
                    b = 1
                elif ch.isdigit():
                    c = 1
            return a + b + c

        types = countTypes(password)
        n = len(password)
        if n < 6:
            return max(6 - n, 3 - types)
        if n <= 20:
            replace = cnt = 0
            prev = '~'
            for curr in password:
                if curr == prev:
                    cnt += 1
                else:
                    replace += cnt // 3
                    cnt = 1
                    prev = curr
            replace += cnt // 3
            return max(replace, 3 - types)
        replace = cnt = 0
        remove, remove2 = n - 20, 0
        prev = '~'
        for curr in password:
            if curr == prev:
                cnt += 1
            else:
                if remove > 0 and cnt >= 3:
                    if cnt % 3 == 0:
                        remove -= 1
                        replace -= 1
                    elif cnt % 3 == 1:
                        remove2 += 1
                replace += cnt // 3
                cnt = 1
                prev = curr
        if remove > 0 and cnt >= 3:
            if cnt % 3 == 0:
                remove -= 1
                replace -= 1
            elif cnt % 3 == 1:
                remove2 += 1
        replace += cnt // 3
        use2 = min(replace, remove2, remove // 2)
        replace -= use2
        remove -= use2 * 2

        use3 = min(replace, remove // 3)
        replace -= use3
        remove -= use3 * 3
        return n - 20 + max(replace, 3 - types)
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Look for the candidate’s understanding of greedy algorithms and the ability to identify the most efficient solution to achieve password strength.

  • question_mark

    Check if the candidate handles edge cases, such as passwords already strong, too short, or containing invalid characters.

  • question_mark

    Evaluate how the candidate approaches the problem using invariant validation while considering each password condition separately.

warning

常见陷阱

外企场景
  • error

    Forgetting to handle edge cases, such as passwords shorter than 6 characters or those that already meet all conditions.

  • error

    Incorrectly applying character type checks, which may lead to unnecessary changes or missed modifications.

  • error

    Not considering efficient handling of consecutive repeating characters, which could lead to unnecessary deletions or changes.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Different constraints could be introduced, such as requiring a password to contain special characters, changing the acceptable length range, or adding more character type checks.

  • arrow_right_alt

    Handling passwords with non-alphanumeric characters like punctuation marks could be an additional challenge, testing edge case handling.

  • arrow_right_alt

    The problem could be modified to handle passwords that must meet different password strength levels, introducing more complex validation conditions.

help

常见问题

外企场景

强密码检验器题解:贪心·invariant | LeetCode #420 困难