Skip to content

Latest commit

 

History

History
183 lines (154 loc) · 4.89 KB

File metadata and controls

183 lines (154 loc) · 4.89 KB

中文文档

Description

A password is said to be strong if it satisfies all the following criteria:

  • It has at least 8 characters.
  • It contains at least one lowercase letter.
  • It contains at least one uppercase letter.
  • It contains at least one digit.
  • It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+".
  • It does not contain 2 of the same character in adjacent positions (i.e., "aab" violates this condition, but "aba" does not).

Given a string password, return true if it is a strong password. Otherwise, return false.

 

Example 1:

Input: password = "IloveLe3tcode!"
Output: true
Explanation: The password meets all the requirements. Therefore, we return true.

Example 2:

Input: password = "Me+You--IsMyDream"
Output: false
Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.

Example 3:

Input: password = "1aB!"
Output: false
Explanation: The password does not meet the length requirement. Therefore, we return false.

 

Constraints:

  • 1 <= password.length <= 100
  • password consists of letters, digits, and special characters: "!@#$%^&*()-+".

Solutions

Python3

class Solution:
    def strongPasswordCheckerII(self, password: str) -> bool:
        if len(password) < 8:
            return False
        ans = 0
        for i, c in enumerate(password):
            if i and password[i - 1] == c:
                return False
            if c.islower():
                ans |= 1
            elif c.isupper():
                ans |= 2
            elif c.isdigit():
                ans |= 4
            else:
                ans |= 8
        return ans == 15

Java

class Solution {
    public boolean strongPasswordCheckerII(String password) {
        if (password.length() < 8) {
            return false;
        }
        int ans = 0;
        char prev = '.';
        for (char c : password.toCharArray()) {
            if (prev == c) {
                return false;
            }
            prev = c;
            if (Character.isLowerCase(c)) {
                ans |= 1;
            } else if (Character.isUpperCase(c)) {
                ans |= 2;
            } else if (Character.isDigit(c)) {
                ans |= 4;
            } else {
                ans |= 8;
            }
        }
        return ans == 15;
    }
}

C++

class Solution {
public:
    bool strongPasswordCheckerII(string password) {
        if (password.size() < 8) return false;
        int ans = 0;
        char prev = '.';
        for (char& c : password) {
            if (c == prev) return false;
            prev = c;
            if (c >= 'a' && c <= 'z')
                ans |= 1;
            else if (c >= 'A' && c <= 'Z')
                ans |= 2;
            else if (c >= '0' && c <= '9')
                ans |= 4;
            else
                ans |= 8;
        }
        return ans == 15;
    }
};

Go

func strongPasswordCheckerII(password string) bool {
	if len(password) < 8 {
		return false
	}
	ans := 0
	for i, c := range password {
		if i > 0 && password[i] == password[i-1] {
			return false
		}
		if unicode.IsLower(c) {
			ans |= 1
		} else if unicode.IsUpper(c) {
			ans |= 2
		} else if unicode.IsDigit(c) {
			ans |= 4
		} else {
			ans |= 8
		}
	}
	return ans == 15
}

TypeScript

function strongPasswordCheckerII(password: string): boolean {
    if (password.length < 8) return false;
    if (!/[a-z]+/g.test(password)) return false;
    if (!/[A-Z]+/g.test(password)) return false;
    if (!/[0-9]+/g.test(password)) return false;
    if (!/[\!\@\#\$\%\^\&\*\(\)\-\+]+/g.test(password)) return false;
    const n = password.length;
    for (let i = 1; i < n; i++) {
        if (password.charAt(i) == password.charAt(i - 1)) return false;
    }
    return true;
}

...