숫자 문자열과 영단어
문제
https://programmers.co.kr/learn/courses/30/lessons/81301
코딩테스트 연습 - 숫자 문자열과 영단어
네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자
programmers.co.kr
코드
import java.util.HashMap;
class Solution {
public int solution(String s) {
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(0, "zero");
map.put(1, "one");
map.put(2, "two");
map.put(3, "three");
map.put(4, "four");
map.put(5, "five");
map.put(6, "six");
map.put(7, "seven");
map.put(8, "eight");
map.put(9, "nine");
char[] s_arr = s.toCharArray();
String result = "";
String temp = "";
for (int i = 0; i < s_arr.length; i++) {
temp += s_arr[i];
if (s_arr[i] < '0' || s_arr[i] > '9') {
for (int key : map.keySet()) {
if (temp.equals(map.get(key))) {
result += key;
temp = "";
break;
}
}
} else {
result += s_arr[i];
temp = "";
}
}
System.out.println(result);
return Integer.parseInt(result);
}
}
'프로그래머스 > LEVEL 1' 카테고리의 다른 글
[프로그래머스/LEVEL1] 신규 아이디 추천 (0) | 2022.01.18 |
---|---|
[프로그래머스/LEVEL1] 시저 암호 (0) | 2022.01.18 |
[프로그래머스/LEVEL1] 소수 만들기 (0) | 2022.01.18 |
[프로그래머스/LEVEL1] 문자열 내림차순으로 배치하기 (0) | 2022.01.18 |
[프로그래머스/LEVEL1] 문자열 내 마음대로 정렬하기 (0) | 2022.01.17 |