Skip to content

Latest commit

 

History

History

2299

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

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: "!@#$%^&*()-+".

Related Topics:
String

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/strong-password-checker-ii
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class Solution {
public:
    bool strongPasswordCheckerII(string s) {
        if (s.size() < 8) return false;
        int lower = 0, upper = 0, digit = 0, special = 0, N = s.size();
        string specialChars = "!@#$%^&*()-+";
        for (int i = 0; i < N; ++i) {
            if (islower(s[i])) ++lower;
            else if (isupper(s[i])) ++upper;
            else if (isdigit(s[i])) ++digit;
            else if (specialChars.find(s[i]) != string::npos) ++special;
            if (i > 0 && s[i] == s[i - 1]) return false;
        }
        return lower && upper && digit && special;
    }
};