2299. Strong Password Checker II LeetCode Solution

In this guide, you will get 2299. Strong Password Checker II LeetCode Solution with the best time and space complexity. The solution to Strong Password Checker II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Strong Password Checker II solution in C++
  4. Strong Password Checker II solution in Java
  5. Strong Password Checker II solution in Python
  6. Additional Resources
2299. Strong Password Checker II LeetCode Solution image

Problem Statement of Strong Password Checker II

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.

See also  1722. Minimize Hamming Distance After Swap Operations LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

2299. Strong Password Checker II LeetCode Solution in C++

class Solution {
 public:
  bool strongPasswordCheckerII(string password) {
    if (password.length() < 8)
      return false;

    const bool hasLowerCase =
        ranges::any_of(password, [](const char c) { return islower(c); });
    if (!hasLowerCase)
      return false;

    const bool hasUpperCase =
        ranges::any_of(password, [](const char c) { return isupper(c); });
    if (!hasUpperCase)
      return false;

    const bool hasDigit =
        ranges::any_of(password, [](const char c) { return isdigit(c); });
    if (!hasDigit)
      return false;

    const bool hasSpecial = ranges::any_of(password, [](const char c) {
      return string("!@#$%^&*()-+").find(c) != string::npos;
    });
    if (!hasSpecial)
      return false;

    for (int i = 1; i < password.length(); ++i)
      if (password[i] == password[i - 1])
        return false;
    return true;
  }
};
/* code provided by PROGIEZ */

2299. Strong Password Checker II LeetCode Solution in Java

class Solution {
  public boolean strongPasswordCheckerII(String password) {
    if (password.length() < 8)
      return false;

    final boolean hasLowerCase = password.chars().anyMatch(c -> Character.isLowerCase(c));
    if (!hasLowerCase)
      return false;

    final boolean hasUpperCase = password.chars().anyMatch(c -> Character.isUpperCase(c));
    if (!hasUpperCase)
      return false;

    final boolean hasDigit = password.chars().anyMatch(c -> Character.isDigit(c));
    if (!hasDigit)
      return false;

    final boolean hasSpecial = password.chars().anyMatch(c -> "!@#$%^&*()-+".indexOf(c) != -1);
    if (!hasSpecial)
      return false;

    for (int i = 1; i < password.length(); ++i)
      if (password.charAt(i) == password.charAt(i - 1))
        return false;
    return true;
  }
}
// code provided by PROGIEZ

2299. Strong Password Checker II LeetCode Solution in Python

class Solution:
  def strongPasswordCheckerII(self, password: str) -> bool:
    if len(password) < 8:
      return False
    if not any(c.islower() for c in password):
      return False
    if not any(c.isupper() for c in password):
      return False
    if not any(c.isdigit() for c in password):
      return False
    if not any("!@#$%^&*()-+".find(c) != -1 for c in password):
      return False
    return all(a != b for a, b in zip(password, password[1:]))
# code by PROGIEZ

Additional Resources

See also  1275. Find Winner on a Tic Tac Toe Game LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.