1 回答

TA貢獻1871條經驗 獲得超8個贊
只需遍歷每個字母并檢查下一個字母是否與當前字母相同,少一個或多一個。所以你可以創造一個獨特的分數。
對于最后一種情況,您可以檢查是否有重復字符并從樂譜中刪除。
$passwords = ['123456', '123456789', 'abcde', '98764321', '101010', '202020', 'xhdgszu'];
foreach ($passwords as $password) {
$score = $length = strlen($password);
$chars = str_split($password);
for ($i = 0; $i < $length - 1; $i++) {
$currentChar = $chars[$i];
$nextChar = $chars[$i + 1];
if ($currentChar === $nextChar
|| ord($currentChar) - 1 === ord($nextChar)
|| ord($currentChar) + 1 === ord($nextChar)
) {
$score--;
continue;
}
}
$unique = array_unique($chars);
$score -= $length - count($unique);
echo "{$password}: {$score} / {$length} ", ($score < $length) ? 'Bad' : 'Good', PHP_EOL;
}
123456: 1 / 6 Bad
123456789: 1 / 9 Bad
abcde: 1 / 5 Bad
98764321: 2 / 8 Bad
101010: -3 / 6 Bad
202020: 2 / 6 Bad
xhdgszu: 7 / 7 Good
- 1 回答
- 0 關注
- 164 瀏覽
添加回答
舉報