study/프로그래머스
[프로그래머스] 숫자 문자열과 영단어
SHplusR
2023. 10. 30. 22:48
접근법 :
**1) 어떻게 풀 것인가?**
간단한 문제같은데 생각보다 조금 헤맸다...
문자열 -> 숫자로 바꾸면 되는 문제
**2) 시간복잡도**
**3) 공간복잡도**
**4) 풀면서 놓쳤던점**
딱히 없음
**5) 이 문제를 통해 얻어갈 것**
코드를 내 생각의 흐름대로 적었다.
다른 사람의 풀이를 보니
String.replaceAll()를 쓴 풀이가 가장 인기있었다.
이번에 replaceAll()을 알게되는 기회가 되었다.
class Solution {
static String[] numbers;
static String answer;
public int solution(String s) {
answer = "";
String temp ="";
numbers = new String[]{"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
for(int i=0; i<s.length(); i++){
if((int)s.charAt(i)>=97){
temp += s.charAt(i);
if(temp.length()>=3){
if(numCompare(temp)){
temp="";
}
}
}
else{
answer += s.charAt(i);
}
}
return Integer.parseInt(answer);
}
static boolean numCompare(String temp){
for(int j=0; j<numbers.length; j++){
if(temp.equals(numbers[j])){
answer += String.valueOf(j);
return true;
}
}
return false;
}
}