숫자 문자열과 영단어

HJ Kwon·2021년 12월 17일
0
  • 함수형 프로그래밍에 대해 가볍게 익힐 수 있는 문제.
  • 처음에는 너무나도 C 스럽게 idx계산으로 접근하였다.
  • 위 경우 문자열 길이에 따라 최악으로 O(N)이 걸릴 수 있겠다.
class Solution {
       fun solution(s: String): Int  = s
        .replace("one", "1")
        .replace("two", "2")
        .replace("three", "3")
        .replace("four", "4")
        .replace("five", "5")
        .replace("six", "6")
        .replace("seven", "7")
        .replace("eight", "8")
        .replace("nine", "9")
        .replace("zero", "0")
        .toInt()
}

0개의 댓글