[codility/java] 2015 Contest - LongestPassword

jyleever·2022년 8월 12일
0

알고리즘

목록 보기
24/26

문제

https://app.codility.com/programmers/trainings/1/longest_password/
You would like to set a password for a bank account. However, there are three restrictions on the format of the password:

it has to contain only alphanumerical characters (a−z, A−Z, 0−9);
there should be an even number of letters;
there should be an odd number of digits.
You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.

For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).

Write a function:

class Solution { public int solution(String S); }

that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.

For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.

Assume that:

N is an integer within the range [1..200];
string S consists only of printable ASCII characters and spaces.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.

풀이

무조건 알파벳+숫자만 포함
문자 개수는 짝수 개
숫자 개수는 홀수 개

parameter
N개의 문자로 구성된 문자열

return
유효한 가장 긴 비밀번호 문자열의 문자 개수
(만약 그러한 조건에 맞는 문자가 없다면 -1 출력)

유효한 비밀번호 문자열 조건

  • 특수문자가 없음
  • 숫자 개수는 홀수 개
  • 문자 개수는 짝수 개

예외처리 잘 확인하기!

코드

/**
무조건 알파벳+숫자만 포함
문자 개수는 짝수 개
숫자 개수는 홀수 개

parameter
N개의 문자로 구성된 문자열

return
유효한 가장 긴 문자열의 문자 개수
(만약 그러한 조건에 맞는 문자가 없다면 -1 출력)

유효한 비밀번호 조건
- 특수문자가 없음
- 숫자 개수는 홀수 개
- 문자 개수는 짝수 개

**/
class Solution {
    public int solution(String S) {
        
        int answer = -1;
        String[] strArr = S.split(" ");

        for(String str : strArr){

            char[] tmp = str.toCharArray();
            int charCnt = 0;
            int digitCnt = 0;
            boolean checkValid = true;

            for(int i=0; i<tmp.length; i++){
                if(('a' <= tmp[i] && 'z' >= tmp[i]) || ('A' <= tmp[i] && 'Z' >= tmp[i])){
                    charCnt++;
                } else if(0 <= tmp[i]-48 && 9 >= tmp[i]-48){
                    digitCnt++;
                } else{
                    // 특수문자인 경우
                    checkValid = false;
                    break;
                }
            }

            if(!checkValid) continue; // 특수문자가 포함된 거라면 유효하지 않음
            else{
                // 유효한지 확인
                if(charCnt % 2 == 1 || digitCnt % 2 == 0) checkValid = false;
                else{
                    checkValid = true;
                    answer = Math.max(answer, str.length());
                }
            }
        }

        return answer;
    }
}

0개의 댓글