[LeetCode] Password Strength

3941. Password Strength

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int passwordStrength(string password) {
unordered_set<char> lo, up, di, sp;
for(auto& p : password) {
if(isdigit(p)) di.insert(p);
else if(isalpha(p)) {
if(islower(p)) lo.insert(p);
else up.insert(p);
} else sp.insert(p);
}
return lo.size() + 2 * up.size() + 3 * di.size() + 5 * sp.size();
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/password-strength/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.