[프로그래머스/Java] 모음 사전

daily_study_78·2021년 10월 29일
0

알고리즘

목록 보기
2/11

문제 링크

https://programmers.co.kr/learn/courses/30/lessons/84512


문제


해결 과정

  • "I"가 1536번째 이므로 "E"는 781(1536/2)번째이다.
  • 5번째 자리의 모음이 변경될 때는 +1, 4번째 자리의 모음이 변경될 때는 +6, 1번째 자리의 모음이 변경될 때는 +781이다.
  • 규칙을 찾아보자
781?????? 6 1
  • 3칸만에 6 -> 781이 되었기 때문에 특정값을 곱했을 것이다. 이때, 1->6이므로 1+5=6이므로 기존 값 * 5 + 1 을 하게되면 781 156 31 6 1 로 규칙이 나온다.

최종 코드

public class Solution {
    public int solution(String word) {
        // 입력되는 word의 길이부터 시작해서 계산하자
        int answer = word.length();
        
        String str = "AEIOU";
        int[] x = {781, 156, 31, 6, 1};
        
        for (int i=0; i<word.length(); i++) {
            int idx = str.indexOf(word.charAt(i));
            answer += x[i] * idx;
            System.out.println(answer);
        }
        
        return answer;
    }
}

0개의 댓글

관련 채용 정보