数字文字列と英語の単語


2021カカオ招聘連絡型実習>数字文字列と英語単語
https://programmers.co.kr/learn/courses/30/lessons/81301
問題の概要
アルファベットを数字に変換
私の答え
#1 replace()の使用
class 숫자문자열과영단어Solution1 {

	public int solution(String s) {
		String[] englishWord = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
		for (int i = 0; i < englishWord.length; i++) {
			s = s.replace(englishWord[i], String.valueOf(i));
		}
		return Integer.parseInt(s);
	}
}
#2 try{}catch(){}の使用
class 숫자문자열과영단어Solution2 {

	public int solution(String s) {
		String[] englishWord = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
		String answer = "";
		String[] ssplit = s.split("");
		String temp = "";
		for (int i = 0; i < ssplit.length; i++) {
			try {
				Integer.parseInt(ssplit[i]);
				answer += ssplit[i];
			} catch (Exception e) {
				temp += ssplit[i];
			}
			for (int j = 0; j < englishWord.length; j++) {
				if (temp.equals(englishWord[j])) {
					answer += String.valueOf(j);
					temp = "";
				}
			}
		}
		return Integer.parseInt(answer);
	}
}
https://github.com/lejehwan/Algorithm/blob/master/Algorithm/src/programmersLevel1/%EC%88%AB%EC%9E%90%EB%AC%B8%EC%9E%90%EC%97%B4%EA%B3%BC%EC%98%81%EB%8B%A8%EC%96%B4.java