algoadvance

Leetcode 2299. Strong Password Checker II

Problem Statement

Leetcode Problem 2299: Strong Password Checker II

A password is considered strong if the below conditions are all met:

  1. It has at least 8 characters.
  2. It contains at least one lowercase letter.
  3. It contains at least one uppercase letter.
  4. It contains at least one digit.
  5. It contains at least one special character. The special characters are: !@#$%^&*()-+
  6. 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.

Clarifying Questions

  1. Input Type: Is the password always a non-empty string?
    • Yes, it can be assumed to be a non-empty string.
  2. Case Sensitivity: Should the password be case-sensitive for uppercase and lowercase checks?
    • Yes, the password should differentiate between uppercase and lowercase letters for the checks.
  3. Special Characters: Are there any other special characters that need to be considered outside of !@#$%^&*()-+?
    • No, only the listed special characters should be considered.

Strategy

  1. Initial Check: First, check the length of the password. If it is less than 8 characters, immediately return false.
  2. Character Classification: Use boolean flags to keep track of the presence of:
    • At least one lowercase letter.
    • At least one uppercase letter.
    • At least one digit.
    • At least one special character.
  3. Adjacent Character Check: Traverse the password to ensure no two adjacent characters are the same.
  4. Final Check: If all conditions are met, return true; otherwise, return false.

Code

public class StrongPasswordChecker {
    public boolean strongPasswordCheckerII(String password) {
        if (password.length() < 8) return false;
        
        boolean hasLowerCase = false;
        boolean hasUpperCase = false;
        boolean hasDigit = false;
        boolean hasSpecialChar = false;
        
        String specialCharacters = "!@#$%^&*()-+";
        
        for (int i = 0; i < password.length(); i++) {
            char ch = password.charAt(i);
            
            // Check for lowercase letter
            if (Character.isLowerCase(ch)) {
                hasLowerCase = true;
            }
            
            // Check for uppercase letter
            if (Character.isUpperCase(ch)) {
                hasUpperCase = true;
            }
            
            // Check for digit
            if (Character.isDigit(ch)) {
                hasDigit = true;
            }
            
            // Check for special character
            if (specialCharacters.indexOf(ch) != -1) {
                hasSpecialChar = true;
            }
            
            // Check for the same character in adjacent positions
            if (i > 0 && ch == password.charAt(i - 1)) {
                return false;
            }
        }
        
        return hasLowerCase && hasUpperCase && hasDigit && hasSpecialChar;
    }
}

Time Complexity

Cut your prep time in half and DOMINATE your interview with AlgoAdvance AI