코린이의 소소한 공부노트

[LeetCode/Easy] 2299. Strong Password Checker II 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 2299. Strong Password Checker II

무지맘 2023. 1. 16. 01:25

1. Input

1) String password

 

2. Output

1) password가 아래 조건을 만족한 강력한 비밀번호라면 true, 아니면 false 반환

// 비밀번호가 강력하기 위한 조건

- 길이는 최소 8이어야 한다.

- 영어 대문자는 최소 1개 있어야 한다.

- 영어 소문자는 최소 1개 있어야 한다.

- 숫자는 최소 1개 있어야 한다.

- 특수 문자는 최소 1개 있어야 한다. 사용할 수 있는 특수 문자는 !@#$%^&*()-+ 뿐이다.

- 같은 문자가 2개 붙어 있으면 안 된다.

 

3. Constraint

1) 1 <= password.length <= 100

2) password에는 조건에 언급된 문자 외에 다른 것은 없다.

 

4. Example

Input: password = "IloveLe3tcode!" -> Output: true

Input: password = "Me+You--IsMyDream" -> Output: false

설명:

- 모든 조건을 만족하기 때문에 true

- 숫자가 없고, 같은 문자가 2개 붙어있기 때문에(--) false

 

5. Code

1) 첫 코드(2022/06/27)

if(password.length() < 8)
    return false;

int[] count = {0,0,0,0};
for(int i=0 ; i<password.length() ; i++){
    char c = password.charAt(i);
    if(65 <= c && c <= 90) // upper
        count[0]++;
    else if(97 <= c && c <= 122) // lower
        count[1]++;
    else if(48 <= c && c <= 57) // digit
        count[2]++;
    else if((c+"").matches("[!@#$%^&*\\(\\)\\-+]")) // special
        count[3]++;
    else
        return false;
}

for(int i=0 ; i<=3 ; i++)
    if(count[i]==0)
        return false;

for(int i=0 ; i<password.length()-1 ; i++)
    if(password.charAt(i) == password.charAt(i+1))
        return false;

return true;