Skip to main content
Back to problems
#2299
Easy Algorithms

Strong password checker ii

String
55.3% acceptance
Feb 25, 2026
381
41
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.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn strong_password_checker_ii(password: String) -> bool {
    let bytes = password.as_bytes();
    if bytes.len() < 8 { return false; }
    let special = b"!@#$%^&*()-+";
    let (mut lower, mut upper, mut digit, mut spec) = (false, false, false, false);
    for i in 0..bytes.len() {
      let b = bytes[i];
      if i > 0 && b == bytes[i - 1] { return false; }
      if b.is_ascii_lowercase() { lower = true; }
      else if b.is_ascii_uppercase() { upper = true; }
      else if b.is_ascii_digit() { digit = true; }
      else if special.contains(&b) { spec = true; }
    }
    lower && upper && digit && spec
  }
}