3941. Password Strength
Description
You are given a string password.
The strength of the password is calculated based on the following rules:
- 1 point for each distinct lowercase letter (
'a'to'z'). - 2 points for each distinct uppercase letter (
'A'to'Z'). - 3 points for each distinct digit (
'0'to'9'). - 5 points for each distinct special character from the set
"!@#$".
Each character contributes at most once, even if it appears multiple times.
Return an integer denoting the strength of the password.
Β
Example 1:
Input: password = "aA1!"
Output: 11
Explanation:
- The distinct characters are
'a','A','1'and'!'. - Thus, the
strength = 1 + 2 + 3 + 5 = 11.
Example 2:
Input: password = "bbB11#"
Output: 11
Explanation:
- The distinct characters are
'b','B','1'and'#'. - Thus, the
strength = 1 + 2 + 3 + 5 = 11.βββββββ
Β
Constraints:
1 <= password.length <= 105passwordconsists of lowercase and uppercase English letters, digits, and special characters from"!@#$".
Solutions
Solution 1: Hash Table
We store each character in the input string in a hash set \(\textit{st}\), so we can quickly ensure each distinct character is counted only once.
Then, we iterate through each character in \(\textit{st}\) and compute the password strength according to the rules:
- If the character is a lowercase letter ('a' to 'z'), add 1 point.
- If the character is an uppercase letter ('A' to 'Z'), add 2 points.
- If the character is a digit ('0' to '9'), add 3 points.
- If the character is a special character (from the set "!@#$"), add 5 points.
Finally, return the computed password strength.
The time complexity is \(O(n)\), where \(n\) is the length of the input string. The space complexity is \(O(m)\), where \(m\) is the number of distinct characters in the input string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |