首页 > 其他分享 >[LeetCode] 2299. Strong Password Checker II

[LeetCode] 2299. Strong Password Checker II

时间:2023-01-19 06:00:09浏览次数:65  
标签:2299 Password false digit II return password true special

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

强密码检验器 II。

如果一个密码满足以下所有条件,我们称它是一个 强 密码:

它有至少 8 个字符。
至少包含 一个小写英文 字母。
至少包含 一个大写英文 字母。
至少包含 一个数字 。
至少包含 一个特殊字符 。特殊字符为:"!@#$%^&*()-+" 中的一个。
它 不 包含 2 个连续相同的字符(比方说 "aab" 不符合该条件,但是 "aba" 符合该条件)。
给你一个字符串 password ,如果它是一个 强 密码,返回 true,否则返回 false 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/strong-password-checker-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路就是按照规则检查 input 字符串是否满足题目的所有要求。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public boolean strongPasswordCheckerII(String password) {
 3         if (password.length() < 8) {
 4             return false;
 5         }
 6         
 7         boolean lower = false;
 8         boolean upper = false;
 9         boolean digit = false;
10         boolean special = false;
11         // HashSet<Character> set = new HashSet<>();
12         // String s = "!@#$%^&*()-+";
13         
14         for (int i = 0; i < password.length(); i++) {
15             char c = password.charAt(i);
16             if (i > 0 && c == password.charAt(i - 1)) {
17                 return false;
18             }
19             if (Character.isUpperCase(c)) {
20                 upper = true;
21             } else if (Character.isLowerCase(c)) {
22                 lower = true;
23             } else if (Character.isDigit(c)) {
24                 digit = true;
25             } else {
26                 // 除了大小写字母,数字之外,就只剩特殊字符了
27                 special = true;
28             }
29         }
30         return upper && lower && digit && special;
31     }
32 }

 

LeetCode 题目总结

标签:2299,Password,false,digit,II,return,password,true,special
From: https://www.cnblogs.com/cnoodle/p/17060998.html

相关文章