Strong Password Checker II Solution in C++

Problem Description

Ah, the age-old struggle of creating a strong password! You know, the one that’s supposed to keep your online life safe from the nefarious hackers lurking in the shadows. The “Strong Password Checker II” problem on LeetCode is like that overly cautious friend who insists on checking your locks every time you leave the house.

The task is simple: validate a password based on a set of rules. Think of it as a bouncer at a club who won’t let you in unless you meet certain criteria. The password must be at least 8 characters long, contain at least one lowercase letter, one uppercase letter, one digit, and one special character from the set !@#$%^&*()-+. Oh, and if you happen to have two identical characters next to each other, you’re out!

So, if your password is “Password123!”, you might think you’re safe, but the bouncer is shaking his head. “Nope, too predictable!”

Code Solution


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;
  }
};

Approach

The approach taken in this solution is straightforward yet effective. It checks the password against a series of conditions:

  1. Length Check: First, it ensures the password is at least 8 characters long.
  2. Character Type Checks: It then verifies the presence of at least one lowercase letter, one uppercase letter, one digit, and one special character.
  3. Consecutive Characters Check: Finally, it checks for any consecutive identical characters, which would be a big no-no in the password world.

If all these conditions are met, the password is deemed strong!

Time and Space Complexity

Time Complexity: O(n), where n is the length of the password. Each character is checked a constant number of times.

Space Complexity: O(1), as we are using a fixed amount of space regardless of the input size.

Real-World Example

Imagine you’re trying to access your online banking account. You type in "password123" and hit enter, only to be met with a stern warning: "Your password is too weak!" It’s like trying to enter a high-security vault with a flimsy piece of paper. The Strong Password Checker II ensures that your password is as secure as a bank vault, keeping your hard-earned money safe from digital thieves.

Similar Problems

If you enjoyed this problem, you might also like these: