A password is considered strong if below conditions are all met:
- It has at least 6 characters and at most 20 characters.
- It must contain at least one lowercase letter, at least one uppercase letter, and at least one digit.
- It must NOT contain three repeating characters in a row ("...aaa..." is weak, but "...aa...a..." is strong, assuming other conditions are met).
Write a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0.
Insertion, deletion or replace of any one character are all considered as one change.
思路:垃圾题,一点算法都没有。抄的答案;我看不出这里面有什么好考察的,我觉得出这个题的人水平也不怎么高。
class Solution {
public int strongPasswordChecker(String s) {
int one = 0, two = 0, chg = 0, p = 0, l = s.length(), r = 0, up = 0, lo = 0, d = 0;
while (p < l) {
char c = s.charAt(p);
if (Character.isUpperCase(c)) up = 1;
if (Character.isLowerCase(c)) lo = 1;
if (Character.isDigit(c)) d = 1;
if (p > 1 && c == s.charAt(p - 1) && c == s.charAt(p - 2)) {
r = 2;
while (p < l && s.charAt(p) == c) {
p++;
r++;
}
if (r % 3 == 0) one++;
else if(r % 3 == 1) two++;
chg += r / 3;
} else p++;
}
int miss = 3 - up - lo - d;
if (l < 6) {
return Math.max(miss, 6 - l);
} else if (l <= 20) {
return Math.max(miss, chg);
} else{
int del = l - 20;
chg -= Math.min(del, one);
chg -= Math.min(Math.max(del - one, 0), two * 2) / 2;
chg -= Math.max(del - one - 2 * two, 0) / 3;
return del + Math.max(chg, miss);
}
}
}
本文介绍了一种强密码检查算法,旨在确保密码符合特定的安全标准。密码必须包含至少一个小写字母、一个大写字母和一个数字,且长度在6到20个字符之间。此外,密码不能包含三个连续重复的字符。文章提供了一个函数strongPasswordChecker,用于评估输入字符串并返回将该字符串转换为强密码所需的最小更改次数。
219

被折叠的 条评论
为什么被折叠?



