Welcome to Subscribe On Youtube

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 <= 105
  • password consists 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.

  • class Solution {
        public int passwordStrength(String password) {
            var st = password.chars().mapToObj(c -> (char) c).collect(Collectors.toSet());
    
            int ans = 0;
    
            for (char ch : st) {
                if (Character.isLowerCase(ch)) {
                    ans += 1;
                } else if (Character.isUpperCase(ch)) {
                    ans += 2;
                } else if (Character.isDigit(ch)) {
                    ans += 3;
                } else {
                    ans += 5;
                }
            }
    
            return ans;
        }
    }
    
  • class Solution {
    public:
        int passwordStrength(string password) {
            unordered_set<char> st(password.begin(), password.end());
    
            int ans = 0;
    
            for (char ch : st) {
                if (islower(ch)) {
                    ans += 1;
                } else if (isupper(ch)) {
                    ans += 2;
                } else if (isdigit(ch)) {
                    ans += 3;
                } else {
                    ans += 5;
                }
            }
    
            return ans;
        }
    };
    
  • class Solution:
        def passwordStrength(self, password: str) -> int:
            st = set(password)
            ans = 0
            for ch in st:
                if ch.islower():
                    ans += 1
                elif ch.isupper():
                    ans += 2
                elif ch.isdigit():
                    ans += 3
                else:
                    ans += 5
            return ans
    
    
  • func passwordStrength(password string) (ans int) {
    	st := map[rune]struct{}{}
    
    	for _, ch := range password {
    		st[ch] = struct{}{}
    	}
    
    	for ch := range st {
    		switch {
    		case unicode.IsLower(ch):
    			ans += 1
    		case unicode.IsUpper(ch):
    			ans += 2
    		case unicode.IsDigit(ch):
    			ans += 3
    		default:
    			ans += 5
    		}
    	}
    
    	return
    }
    
  • function passwordStrength(password: string): number {
        const st = new Set(password);
    
        let ans = 0;
    
        for (const ch of st) {
            if (/[a-z]/u.test(ch)) {
                ans += 1;
            } else if (/[A-Z]/u.test(ch)) {
                ans += 2;
            } else if (/\d/u.test(ch)) {
                ans += 3;
            } else {
                ans += 5;
            }
        }
    
        return ans;
    }
    
    

All Problems

All Solutions