Skip to main content

Strong Password Checker II - Video Solutions

EasyString

LeetCode 2299 – How to Check Strong Passwords in Java (Easy Explanation)

Recurso
15:44577 views
7 video solutions available

Strong Password Checker II - Video Solution

Watch 7 video solutions for Strong Password Checker II, a easy level problem involving String. This walkthrough by Recurso has 577 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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

Approach Overview

Problem Overview: You are given a password string and must determine if it qualifies as a strong password. A valid password must be at least 8 characters long, contain lowercase, uppercase, digit, and special characters, and must not have identical adjacent characters. The task is essentially a rule validation problem on a string.

Approach 1: Iterative Character Check (O(n) time, O(1) space)

Scan the password once while tracking whether each required category appears. During iteration, check if the current character is lowercase, uppercase, digit, or one of the allowed special characters. Maintain boolean flags for each requirement and immediately reject if two adjacent characters are equal. Because you only iterate through the string once and store a few flags, the solution runs in O(n) time with constant O(1) space.

This approach is straightforward and gives full control over each rule. It works well in interviews because you explicitly show how each condition is validated using simple character checks. The logic mostly relies on basic string traversal and conditional checks.

Approach 2: Regular Expression (O(n) time, O(1) space)

Another option is to validate the password using regular expressions. A regex pattern can enforce the presence of lowercase letters, uppercase letters, digits, and special characters using lookaheads. After the regex match, perform an additional scan to ensure no adjacent characters are identical.

The regex engine still processes the string linearly, so the complexity remains O(n) time with constant O(1) extra space. This method produces shorter code and is convenient when multiple character rules must be validated simultaneously. However, debugging complex regex patterns can be harder during interviews.

Recommended for interviews: The iterative character check approach is usually preferred. It clearly demonstrates how you validate constraints, detect adjacent duplicates, and manage state while scanning a string. Regex solutions are concise but may hide the underlying logic, while the iterative solution shows stronger problem-solving clarity.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterative Character CheckO(n)O(1)Best general solution. Clear rule validation and preferred in coding interviews.
Regular ExpressionO(n)O(1)Useful when combining multiple character checks compactly using regex patterns.