[프로그래머스] 문자 개수 세기

Seah Lee·2023년 6월 29일
0

프로그래머스

목록 보기
52/57

class Solution {
    public int[] solution(String my_string) {
        
        int[] result = new int[52];
        
        for(int i=0;i<my_string.length();i++){
            char a = my_string.charAt(i);
            int cp = (int) a;
            
            if (cp >= 65 && cp <= 90) { 
                result[cp - 65]++;
            } else if (cp >= 97 && cp <= 122) { 
                result[cp - 71]++;
            }  
        }
        
        return result;
        
    }
}

char로 받고 앞에 (int) 씌우면 아스키코드값으로 나온다..

[다른 사람의 풀이]

class Solution {
    public int[] solution(String my_string) {
        int[] answer = new int[52];
        for(int i = 0; i < my_string.length(); i++){
            char c = my_string.charAt(i);
            if(c >= 'a')
                answer[c - 'a' + 26]++;
            else
                answer[c - 'A']++;
        }
        return answer;
    }
}

굳이 내가 아스키값 계산안하고 그냥 char상태에서 바로 빼서 쓸 수도 있었다^^..

profile
성장하는 개발자

0개의 댓글