> 문제설명
leetcode.com/problems/reconstruct-original-digits-from-english/
문제 내용 요약
0부터 9까지의 숫자의 스펠링이 규칙없이 나열된 String에서 숫자를 복원해 오름차순으로 반환한다.
> IDEA
각 숫자가 가질 수 있는 특별한 알파벳을 검사하여 해결할 수 있다.
> Solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
class Solution {
void mapRemove(char c, HashMap<Character, Integer> stringMap){
if(!stringMap.containsKey(c)) return;
if(stringMap.get(c) == 1){
stringMap.remove(c);
} else {
stringMap.replace(c, stringMap.get(c) - 1);
}
}
public String originalDigits(String s) {
ArrayList<Integer> answerList = new ArrayList<>();
HashMap<Character, Integer> stringMap = new HashMap<>();
for(int idx = 0; idx < s.length(); idx++){
if(stringMap.containsKey(s.charAt(idx))){
stringMap.replace(s.charAt(idx), stringMap.get(s.charAt(idx))+1);
} else {
stringMap.put(s.charAt(idx), 1);
}
}
while (!stringMap.isEmpty()){
if(stringMap.containsKey('z')) {
answerList.add(0);
mapRemove('z', stringMap);
mapRemove('e', stringMap);
mapRemove('r', stringMap);
mapRemove('o', stringMap);
continue;
}
if(stringMap.containsKey('w')) {
answerList.add(2);
mapRemove('t', stringMap);
mapRemove('w', stringMap);
mapRemove('o', stringMap);
continue;
}
if(stringMap.containsKey('u')) {
answerList.add(4);
mapRemove('f', stringMap);
mapRemove('o', stringMap);
mapRemove('u', stringMap);
mapRemove('r', stringMap);
continue;
}
if(stringMap.containsKey('x')) {
answerList.add(6);
mapRemove('s', stringMap);
mapRemove('i', stringMap);
mapRemove('x', stringMap);
continue;
}
if(stringMap.containsKey('g')) {
answerList.add(8);
mapRemove('e', stringMap);
mapRemove('i', stringMap);
mapRemove('g', stringMap);
mapRemove('h', stringMap);
mapRemove('t', stringMap);
continue;
}
if(stringMap.containsKey('o')) {
answerList.add(1);
mapRemove('o', stringMap);
mapRemove('n', stringMap);
mapRemove('e', stringMap);
continue;
}
if(stringMap.containsKey('t') || stringMap.containsKey('h') || stringMap.containsKey('r')) {
answerList.add(3);
mapRemove('t', stringMap);
mapRemove('h', stringMap);
mapRemove('r', stringMap);
mapRemove('e', stringMap);
mapRemove('e', stringMap);
continue;
}
if(stringMap.containsKey('f')) {
answerList.add(5);
mapRemove('f', stringMap);
mapRemove('i', stringMap);
mapRemove('v', stringMap);
mapRemove('e', stringMap);
continue;
}
if(stringMap.containsKey('s')) {
answerList.add(7);
mapRemove('s', stringMap);
mapRemove('e', stringMap);
mapRemove('v', stringMap);
mapRemove('e', stringMap);
mapRemove('n', stringMap);
continue;
}
answerList.add(9);
mapRemove('n', stringMap);
mapRemove('i', stringMap);
mapRemove('n', stringMap);
mapRemove('e', stringMap);
continue;
}
Collections.sort(answerList);
StringBuilder sb = new StringBuilder();
for(int num : answerList){
sb.append(num);
}
return sb.toString();
}
}
728x90
반응형
'Algorithms > LeetCode DailyChallenge' 카테고리의 다른 글
[LeetCode] 451. Sort Characters By Frequencyl / JAVA (0) | 2021.10.23 |
---|---|
[LeetCode] 971. Flip Binary Tree To Match Preorder Traversal / JAVA (0) | 2021.03.30 |
[LeetCode] 966. Vowel Spellchecker / JAVA (0) | 2021.03.23 |
[LeetCode] 869. Reordered Power of 2 / JAVA (0) | 2021.03.22 |
[LeetCode] 841. Keys and Rooms / JAVA (0) | 2021.03.20 |