replaceAll
https://school.programmers.co.kr/learn/courses/30/lessons/81301
import java.util.*;
class Solution {
public int solution(String s) {
int answer = 0;
Map<String, Integer> map = initStr();
for(String word : map.keySet()) {
s = s.replaceAll(word, String.valueOf(map.get(word)));
}
return Integer.parseInt(s);
}
public Map initStr() {
Map<String, Integer> map = new HashMap<>();
map.put("zero", 0);
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
map.put("four", 4);
map.put("five", 5);
map.put("six", 6);
map.put("seven", 7);
map.put("eight", 8);
map.put("nine", 9);
return map;
}
}
import java.util.*;
class Solution {
public int solution(String s) {
int answer = 0;
String[] words = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for(int i=0; i<words.length; i++) {
s = s.replaceAll(words[i], String.valueOf(i));
}
return Integer.parseInt(s);
}
}
10분
알고리즘을 풀 때 성능 뿐 아니라, 가독성을 높이기 위한 방법도 고려해보자!