[Programmers] 2021 카카오 채용연계형 인턴십 - 숫자 문자열과 영단어

zzenee·2022년 5월 10일
0

Algorithm&Coding-test

목록 보기
9/30
post-thumbnail

Problem

Code

import java.util.*;

class Solution {
    public int solution(String s) {
        String[] engArr = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        String result = "";
        String text = "";
        for (char ch: s.toCharArray()) {
            if (Character.isDigit(ch)) {
                result += String.valueOf(ch);
            } else {
                text += String.valueOf(ch);
                int textIdx = Arrays.asList(engArr).indexOf(text);
                if (textIdx != -1) {
                    result += String.valueOf(textIdx);
                    text = "";
                }
            }
        }
        return Integer.parseInt(result);
    }
}
profile
꾸준히

0개의 댓글