420. Strong Password Checker
Пароль считается надежным, если выполняются следующие условия: в нем не менее 6 и не более 20 символов. он содержит не менее одной строчной буквы, не менее одной заглавной буквы и не менее одной цифры. он не содержит трех повторяющихся символов подряд (наVí dụ, "Baaabb0" - слабый, а "Baaba0" - сильный). given строку пароля, return минимальное количество шагов, необходимых для того, чтобы сделать пароль сильным. Если пароль уже сильный, return 0. За один шаг можно: вставить один символ в пароль, удалить один символ из пароля или заменить один символ пароля другим символом.
Ví dụ:
Input: password = "a"
Output: 5
C# lời giải
đã khớp/gốcpublic class Solution {
public int StrongPasswordChecker(string s) {
int n = s.Length;
bool hasLower = false, hasUpper = false, hasDigit = false;
int repeatCount = 0;
for (int i = 0; i < n;) {
if (char.IsLower(s[i])) hasLower = true;
if (char.IsUpper(s[i])) hasUpper = true;
if (char.IsDigit(s[i])) hasDigit = true;
int start = i;
while (i < n && s[i] == s[start]) {
i++;
}
repeatCount += (i - start) / 3;
}
int missingTypes = (hasLower ? 0 : 1) + (hasUpper ? 0 : 1) + (hasDigit ? 0 : 1);
if (n < 6) {
return Math.Max(missingTypes, 6 - n);
} else if (n <= 20) {
return Math.Max(missingTypes, repeatCount);
} else {
int excessChars = n - 20;
int overLenReduction = 0;
for (int i = 2; i < n && excessChars > 0; i++) {
if (i % 3 == 2 && s[i] == s[i - 1] && s[i] == s[i - 2]) {
overLenReduction++;
excessChars--;
}
}
repeatCount -= overLenReduction;
return (n - 20) + Math.Max(missingTypes, repeatCount);
}
}
}
C++ lời giải
bản nháp tự động, xem lại trước khi gửi#include <bits/stdc++.h>
using namespace std;
// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
public int StrongPasswordChecker(string s) {
int n = s.size();
bool hasLower = false, hasUpper = false, hasDigit = false;
int repeatCount = 0;
for (int i = 0; i < n;) {
if (char.IsLower(s[i])) hasLower = true;
if (char.IsUpper(s[i])) hasUpper = true;
if (char.IsDigit(s[i])) hasDigit = true;
int start = i;
while (i < n && s[i] == s[start]) {
i++;
}
repeatCount += (i - start) / 3;
}
int missingTypes = (hasLower ? 0 : 1) + (hasUpper ? 0 : 1) + (hasDigit ? 0 : 1);
if (n < 6) {
return max(missingTypes, 6 - n);
} else if (n <= 20) {
return max(missingTypes, repeatCount);
} else {
int excessChars = n - 20;
int overLenReduction = 0;
for (int i = 2; i < n && excessChars > 0; i++) {
if (i % 3 == 2 && s[i] == s[i - 1] && s[i] == s[i - 2]) {
overLenReduction++;
excessChars--;
}
}
repeatCount -= overLenReduction;
return (n - 20) + max(missingTypes, repeatCount);
}
}
}
Java lời giải
đã khớp/gốcpublic class Solution {
public int strongPasswordChecker(String s) {
int n = s.length();
boolean hasLower = false, hasUpper = false, hasDigit = false;
int repeatCount = 0;
for (int i = 0; i < n;) {
if (Character.isLowerCase(s.charAt(i))) hasLower = true;
if (Character.isUpperCase(s.charAt(i))) hasUpper = true;
if (Character.isDigit(s.charAt(i))) hasDigit = true;
int start = i;
while (i < n && s.charAt(i) == s.charAt(start)) {
i++;
}
repeatCount += (i - start) / 3;
}
int missingTypes = (hasLower ? 0 : 1) + (hasUpper ? 0 : 1) + (hasDigit ? 0 : 1);
if (n < 6) {
return Math.max(missingTypes, 6 - n);
} else if (n <= 20) {
return Math.max(missingTypes, repeatCount);
} else {
int excessChars = n - 20;
int overLenReduction = 0;
for (int i = 2; i < n && excessChars > 0; i++) {
if (i % 3 == 2 && s.charAt(i) == s.charAt(i - 1) && s.charAt(i) == s.charAt(i - 2)) {
overLenReduction++;
excessChars--;
}
}
repeatCount -= overLenReduction;
return (n - 20) + Math.max(missingTypes, repeatCount);
}
}
}
JavaScript lời giải
đã khớp/gốcfunction strongPasswordChecker(s) {
const n = s.length;
const hasLower = /[a-z]/.test(s);
const hasUpper = /[A-Z]/.test(s);
const hasDigit = /\d/.test(s);
let repeatCount = 0;
for (let i = 0; i < n;) {
const start = i;
while (i < n && s[i] === s[start]) {
i++;
}
repeatCount += Math.floor((i - start) / 3);
}
const missingTypes = 3 - [hasLower, hasUpper, hasDigit].filter(Boolean).length;
if (n < 6) {
return Math.max(missingTypes, 6 - n);
} else if (n <= 20) {
return Math.max(missingTypes, repeatCount);
} else {
let excessChars = n - 20;
let overLenReduction = 0;
for (let i = 2; i < n && excessChars > 0; i++) {
if (i % 3 === 2 && s[i] === s[i - 1] && s[i] === s[i - 2]) {
overLenReduction++;
excessChars--;
}
}
repeatCount -= overLenReduction;
return (n - 20) + Math.max(missingTypes, repeatCount);
}
}
Python lời giải
đã khớp/gốcdef strongPasswordChecker(s):
n = len(s)
has_lower = any(c.islower() for c in s)
has_upper = any(c.isupper() for c in s)
has_digit = any(c.isdigit() for c in s)
repeat_count = sum((len(list(g)) // 3) for _, g in itertools.groupby(s))
missing_types = 3 - sum([has_lower, has_upper, has_digit])
if n < 6:
return max(missing_types, 6 - n)
elif n <= 20:
return max(missing_types, repeat_count)
else:
excess_chars = n - 20
over_len_reduction = 0
i = 2
while i < n and excess_chars > 0:
if i % 3 == 2 and s[i] == s[i - 1] and s[i] == s[i - 2]:
over_len_reduction += 1
excess_chars -= 1
i += 1
repeat_count -= over_len_reduction
return (n - 20) + max(missing_types, repeat_count)
Go lời giải
đã khớp/gốcpackage main
import (
"unicode"
)
func strongPasswordChecker(s string) int {
n := len(s)
hasLower, hasUpper, hasDigit := false, false, false
repeatCount := 0
for i := 0; i < n; {
if unicode.IsLower(rune(s[i])) {
hasLower = true
}
if unicode.IsUpper(rune(s[i])) {
hasUpper = true
}
if unicode.IsDigit(rune(s[i])) {
hasDigit = true
}
start := i
for i < n && s[i] == s[start] {
i++
}
repeatCount += (i - start) / 3
}
missingTypes := 0
if !hasLower {
missingTypes++
}
if !hasUpper {
missingTypes++
}
if !hasDigit {
missingTypes++
}
if n < 6 {
return max(missingTypes, 6-n)
} else if n <= 20 {
return max(missingTypes, repeatCount)
} else {
excessChars := n - 20
overLenReduction := 0
for i := 2; i < n && excessChars > 0; i++ {
if i%3 == 2 && s[i] == s[i-1] && s[i] == s[i-2] {
overLenReduction++
excessChars--
}
}
repeatCount -= overLenReduction
return (n - 20) + max(missingTypes, repeatCount)
}
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
Algorithm
Определите количество недостающих символов до минимума и превышающих символов для Ràng buộc длины пароля. Также определите наличие строчных, заглавных букв и цифр.
Вычислите количество необходимых замен для устранения трех повторяющихся символов подряд.
Определите минимальное количество шагов для приведения пароля к требуемым условиям, используя вычисленные значения недостающих символов, превышающих символов и замен.
😎
Vacancies for this task
việc làm đang hoạt động with overlapping task tags are đã hiển thị.