기록하는 개발자

[프로그래머스][JAVA] 숫자 문자열과 영단어 본문

Algorithm

[프로그래머스][JAVA] 숫자 문자열과 영단어

밍맹030 2022. 5. 3. 23:08
728x90

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

 

코딩테스트 연습 - 숫자 문자열과 영단어

네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자

programmers.co.kr

 

* replace 함수는 문자열 내 특정 substring을 다른 문자열로 바꿔준다.

 

[ 예시 ]
String s = "aaabbb";
s.replace("aaa", "zzz");
→ s = "zzzbbb"

 

class Solution {
    public int solution(String s) {
    String[] words= {"zero" , "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine"};
    String[] nums= {"0","1","2","3","4","5","6","7","8","9"};

        for (int i = 0 ; i <10 ; i++)
            s = s.replace(words[i] , nums[i]);
        
        return Integer.parseInt(s);
    }
}
728x90