class Solution {
    public int solution(String dartResult) {
        int answer = 0;
        int start = 0;
        int[] score = new int[3];
        
        for(int i=0; i<3; i++) {
            String number = "";
            
            for(int j=start; j<dartResult.length(); j++) {
                char c = dartResult.charAt(j);
                
                if(c>='0' && c<='9') {
                    number += String.valueOf(c);
                    continue;
                }
                
                if(c=='S' || c=='D' || c=='T') {
                    int value = Integer.parseInt(number);
                    score[i] = c=='S' ? value : c=='D' ? (int)(Math.pow(value, 2)) : (int)(Math.pow(value, 3));
                    if(j==dartResult.length()-1 || (dartResult.charAt(j+1)>='0'&&dartResult.charAt(j+1)<='9')) {
                        start = j+1;
                        break;
                    }
                    
                    continue;
                }
                
                if(c=='*') {
                    score[i] *= 2;
                    if(i>0) { score[i-1] *= 2; }
                    start = j+1; break;
                } else if(c=='#') {
                    score[i] *= -1;
                    start = j+1; break;
                }
            }
        }
        
        for(int i=0; i<3; i++) { answer += score[i]; }
        
        return answer;
    }
}

'Problem Solving > Programmers' 카테고리의 다른 글

[Level 1] 문자열 내 마음대로 정렬하기  (0) 2022.03.24
[Level 1] 같은 숫자는 싫어  (0) 2022.03.24
[Level 1] [1차] 비밀지도  (0) 2022.03.24
[Level 1] 최소직사각형  (0) 2022.03.24
[Level 1] 2016년  (0) 2022.03.24