Skip to main content

Strong Password Checker II - Solution & Explanation

EasyString17 min readAsked at: Microsoft
Practice this problem

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

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.

Approach 1: Approach 1: Iterative Character Check

This approach involves iterating through each character of the password while maintaining flags that check for different conditions like uppercase, lowercase, digit, special character, and adjacent duplicates. Start by checking if the length of the password is at least 8. Update flags as you iterate through the password and check if two consecutive characters are the same. If all flags are true and no two consecutive characters are the same, then the password is strong.

The C implementation uses the standard library to check character types and maintain flags to ensure the password meets all conditions without adjacent duplicates.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), Space Complexity: O(1) - iterating through the password once with a fixed number of flags.

Try this approach in the editor →

Approach 2: Approach 2: Regular Expression

This method simplifies many checks by leveraging a regular expression to validate parts of the password rules. The regex checks can determine the presence of character types and length while additional logic ensures no adjacent duplicates are present. This approach makes the code cleaner but requires a good understanding of regex.

C code employs regular expressions for matching multiple criteria in one line, although C's regex library involves additional complexity with compiling and executing expressions.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), Space Complexity: O(1).

Try this approach in the editor →

Approach 3: Simulation + Bit Manipulation

According to the problem description, we can simulate the process of checking whether the password meets the requirements.

First, we check if the length of the password is less than 8. If it is, we return false.

Next, we use a mask mask to record whether the password contains lowercase letters, uppercase letters, digits, and special characters. We traverse the password, and for each character, we first check if it is the same as the previous character. If it is, we return false. Then, we update the mask mask based on the character type. Finally, we check if the mask mask is 15. If it is, we return true; otherwise, we return false.

The time complexity is O(n), and the space complexity is O(1). Here, n is the length of the password.

Code

Python

Java

C++

Go

TypeScript

Rust

C

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Iterative Character Check

Time Complexity: O(n), Space Complexity: O(1) - iterating through the password once with a fixed number of flags.

Approach 2: Regular Expression

Time Complexity: O(n), Space Complexity: O(1).

Simulation + Bit Manipulation

Detailed 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.

Video Solution

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

Watch 6 more video solutions →

Frequently Asked Questions

Is Strong Password Checker II easy or hard?
Strong Password Checker II is classified as an Easy problem. The main challenge is carefully implementing all validation rules—length requirement, character categories, allowed special characters, and preventing consecutive duplicates.
Strong Password Checker II Python/Java solution
Both Python and Java solutions typically implement a single loop over the password string. During iteration they track character categories and check adjacent duplicates. The implementation runs in O(n) time and uses constant extra space.
How to solve Strong Password Checker II in O(n)?
Iterate through the password string once and maintain flags for lowercase, uppercase, digit, and special characters. At each step also check whether the current character equals the previous one. After the scan, confirm that the length is at least 8 and all flags are true.
What is the best approach for Strong Password Checker II?
The best approach is a single-pass iterative character check. Iterate through the string once while tracking whether lowercase, uppercase, digit, and special characters appear, and ensure no adjacent characters are equal. This solution runs in O(n) time with O(1) space and is the most readable for interviews.
Is Strong Password Checker II asked at Google/Amazon/Meta?
Strong Password Checker II is a typical string validation problem similar to interview questions asked by companies like Amazon and Google. It tests attention to detail, rule validation, and efficient single-pass string processing.
What data structure is used in Strong Password Checker II?
The problem mainly uses simple string traversal. No complex data structures are required—only a few boolean flags and character comparisons while iterating through the string.
What is the time complexity of Strong Password Checker II?
The optimal solution runs in O(n) time where n is the length of the password. You scan the string once to validate all rules. Space complexity is O(1) because only a few boolean flags are stored.

Ready to solve this problem?

Practice Strong Password Checker II with our built-in code editor and test cases.

Practice on FleetCode