[LeetCode] Strong Password Checker

420. Strong Password Checker

A password is considered strong if the below conditions are all met:

  • It has at least 6 characters and at most 20 characters.
  • It contains at least one lowercase letter, at least one uppercase letter, and at least one digit.
  • It does not contain three repeating characters in a row (i.e., “…aaa…” is weak, but “…aa…a…” is strong, assuming other conditions are met).

Given a string password, return the minimum number of steps required to make password strong. if password is already strong, return 0.

In one step, you can:

  • Insert one character to password,
  • Delete one character from password, or
  • Replace one character of password with another character.
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
int dp[2][2][2][51][21][3][256];
class Solution {

int helper(string& s, int p, int l, int u, int d, int choice, char prev, int conseqtive) {
if(choice > 20) {return 987654321;}
if(dp[l][u][d][p][choice][conseqtive][prev] != -1) return dp[l][u][d][p][choice][conseqtive][prev];
int& res = dp[l][u][d][p][choice][conseqtive][prev] = 987654321;
if(s.length() == p) {return res = max(max(0, 6 - choice),(3 - l - u - d));}

int nConsequtive = prev == s[p] ? conseqtive + 1 : 1;

if(nConsequtive != 3) {
bool nl = l | islower(s[p]);
bool nu = u | isupper(s[p]);
bool nd = d | isdigit(s[p]);

res = min(res, helper(s, p + 1, nl, nu, nd, choice + 1, s[p], nConsequtive));
}

//delete
res = min(res, helper(s, p + 1, l, u, d, choice, prev, conseqtive) + 1);

//modify
res = min(res, helper(s, p + 1, l, u, 1, choice + 1, '#', 1) + 1);
res = min(res, helper(s, p + 1, 1, u, d, choice + 1, '#', 1) + 1);
res = min(res, helper(s, p + 1, l, 1, d, choice + 1, '#', 1) + 1);


if(nConsequtive == 3) {
res = min(res, helper(s, p, 1, u, d, choice + 1, '#', 1) + 1);
res = min(res, helper(s, p, l, 1, d, choice + 1, '#', 1) + 1);
res = min(res, helper(s, p, l, u, 1, choice + 1, '#', 1) + 1);
}

return res;
}
public:
int strongPasswordChecker(string password) {
memset(dp,-1,sizeof(dp));
return helper(password, 0, 0, 0, 0, 0, '#', 1);
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/03/11/PS/LeetCode/strong-password-checker/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.