ITMISS
只需遍历每个字母并检查下一个字母是否与当前字母相同,少一个或多一个。所以你可以创造一个独特的分数。对于最后一种情况,您可以检查是否有重复字符并从乐谱中删除。$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 Bad123456789: 1 / 9 Badabcde: 1 / 5 Bad98764321: 2 / 8 Bad101010: -3 / 6 Bad202020: 2 / 6 Badxhdgszu: 7 / 7 Good