返回介绍

solution / 2200-2299 / 2299.Strong Password Checker II / README_EN

发布于 2024-06-17 01:03:07 字数 6454 浏览 0 评论 0 收藏 0

2299. Strong Password Checker II

中文文档

Description

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

Solutions

Solution 1

class Solution:
  def strongPasswordCheckerII(self, password: str) -> bool:
    if len(password) < 8:
      return False
    mask = 0
    for i, c in enumerate(password):
      if i and c == password[i - 1]:
        return False
      if c.islower():
        mask |= 1
      elif c.isupper():
        mask |= 2
      elif c.isdigit():
        mask |= 4
      else:
        mask |= 8
    return mask == 15
class Solution {
  public boolean strongPasswordCheckerII(String password) {
    if (password.length() < 8) {
      return false;
    }
    int mask = 0;
    for (int i = 0; i < password.length(); ++i) {
      char c = password.charAt(i);
      if (i > 0 && c == password.charAt(i - 1)) {
        return false;
      }
      if (Character.isLowerCase(c)) {
        mask |= 1;
      } else if (Character.isUpperCase(c)) {
        mask |= 2;
      } else if (Character.isDigit(c)) {
        mask |= 4;
      } else {
        mask |= 8;
      }
    }
    return mask == 15;
  }
}
class Solution {
public:
  bool strongPasswordCheckerII(string password) {
    if (password.size() < 8) {
      return false;
    }
    int mask = 0;
    for (int i = 0; i < password.size(); ++i) {
      char c = password[i];
      if (i && c == password[i - 1]) {
        return false;
      }
      if (c >= 'a' && c <= 'z') {
        mask |= 1;
      } else if (c >= 'A' && c <= 'Z') {
        mask |= 2;
      } else if (c >= '0' && c <= '9') {
        mask |= 4;
      } else {
        mask |= 8;
      }
    }
    return mask == 15;
  }
};
func strongPasswordCheckerII(password string) bool {
  if len(password) < 8 {
    return false
  }
  mask := 0
  for i, c := range password {
    if i > 0 && byte(c) == password[i-1] {
      return false
    }
    if unicode.IsLower(c) {
      mask |= 1
    } else if unicode.IsUpper(c) {
      mask |= 2
    } else if unicode.IsDigit(c) {
      mask |= 4
    } else {
      mask |= 8
    }
  }
  return mask == 15
}
function strongPasswordCheckerII(password: string): boolean {
  if (password.length < 8) {
    return false;
  }
  let mask = 0;
  for (let i = 0; i < password.length; ++i) {
    const c = password[i];
    if (i && c == password[i - 1]) {
      return false;
    }
    if (c >= 'a' && c <= 'z') {
      mask |= 1;
    } else if (c >= 'A' && c <= 'Z') {
      mask |= 2;
    } else if (c >= '0' && c <= '9') {
      mask |= 4;
    } else {
      mask |= 8;
    }
  }
  return mask == 15;
}
impl Solution {
  pub fn strong_password_checker_ii(password: String) -> bool {
    let s = password.as_bytes();
    let n = password.len();
    if n < 8 {
      return false;
    }
    let mut mask = 0;
    let mut prev = b' ';
    for &c in s.iter() {
      if c == prev {
        return false;
      }
      mask |= if c.is_ascii_uppercase() {
        0b1000
      } else if c.is_ascii_lowercase() {
        0b100
      } else if c.is_ascii_digit() {
        0b10
      } else {
        0b1
      };
      prev = c;
    }
    mask == 0b1111
  }
}
bool strongPasswordCheckerII(char* password) {
  int n = strlen(password);
  if (n < 8) {
    return false;
  }
  int mask = 0;
  char prev = ' ';
  for (int i = 0; i < n; i++) {
    if (prev == password[i]) {
      return false;
    }
    if (islower(password[i])) {
      mask |= 0b1000;
    } else if (isupper(password[i])) {
      mask |= 0b100;
    } else if (isdigit(password[i])) {
      mask |= 0b10;
    } else {
      mask |= 0b1;
    }
    prev = password[i];
  }
  return mask == 0b1111;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文