๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์ž๋ฃŒ๊ตฌ์กฐ & ์•Œ๊ณ ๋ฆฌ์ฆ˜

์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด (Java)

by 1mj 2021. 10. 26.

https://programmers.co.kr/learn/courses/30/lessons/81301

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด

๋„ค์˜ค์™€ ํ”„๋กœ๋„๊ฐ€ ์ˆซ์ž๋†€์ด๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋„ค์˜ค๊ฐ€ ํ”„๋กœ๋„์—๊ฒŒ ์ˆซ์ž๋ฅผ ๊ฑด๋„ฌ ๋•Œ ์ผ๋ถ€ ์ž๋ฆฟ์ˆ˜๋ฅผ ์˜๋‹จ์–ด๋กœ ๋ฐ”๊พผ ์นด๋“œ๋ฅผ ๊ฑด๋„ค์ฃผ๋ฉด ํ”„๋กœ๋„๋Š” ์›๋ž˜ ์ˆซ์ž๋ฅผ ์ฐพ๋Š” ๊ฒŒ์ž„์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ์ˆซ์ž์˜ ์ผ๋ถ€ ์ž

programmers.co.kr

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2021 ์นด์นด์˜ค ์ฑ„์šฉ์—ฐ๊ณ„ํ˜• ์ธํ„ด์‹ญ "์ˆซ์ž ๋ฌธ์ž์—ด๊ณผ ์˜๋‹จ์–ด" ๋ฌธ์ œ ํ’€์ด์ด๋‹ค.

์˜๋‹จ์–ด๋กœ ๋œ ์ˆซ์ž๊ฐ€ ์–ด๋””์— ์žˆ๋Š”์ง€ ์ฐพ๊ธฐ๋งŒ ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 

import java.util.*;

class Solution {
    static final String[] ENG = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    static final String[] NUM = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    
    public int solution(String s) {
        String result = "";
        String str = "";
        for (int i = 0; i < s.length(); i++) {
            // ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๊ธฐ ์ „๊นŒ์ง€ ์ด์ „ ์˜๋‹จ์–ด๋“ค์„ str์— ์ €์žฅ
            if (!isNumeric(s.charAt(i))) {
                str += s.charAt(i);
            } else {
                // ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๋ฉด ์ €์žฅ๋œ ์˜๋‹จ์–ด๋“ค์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
                result += getParseNumber(str);
                result += s.charAt(i);
                str = "";
            }
            
            // ์ˆซ์ž๊ฐ€ ๋”์ด์ƒ ๋‚˜์˜ค์ง€ ์•Š์œผ๋ฉด(์˜๋‹จ์–ด๋กœ ๋๋‚˜๋ฉด) ๊ทธ ์˜๋‹จ์–ด๋“ค์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
            if (i == s.length() - 1 && !isNumeric(s.charAt(i))) {
                result += getParseNumber(str);
            }
        }
        return Integer.parseInt(result);
    }
    
    // ๋ฌธ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธ
    // ex) '0' -> 0
    public static boolean isNumeric(char c) {
        int num = Character.getNumericValue(c);
        if (num >= 0 && num <= 9) { // 0 ~ 9 ์‚ฌ์ด์— ์žˆ์œผ๋ฉด ์ˆซ์ž
            return true;
        }
        return false;
    }
    
    // ์˜๋‹จ์–ด๋กœ๋œ ์ˆซ์ž๋ฅผ ์ˆซ์ž๋กœ ๋ณ€ํ™˜
    // ex) sevensix -> 76
    public static String getParseNumber(String str) {
        if (str.length() == 0) return "";
        
        String result = "";
        String temp = "";
        int idx = 0;
        for (int i = 0; i < str.length(); i++) {
            temp += str.charAt(i);
            // ์˜๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ์ผ ์ˆ˜๋„ ์žˆ์œผ๋‹ˆ ํ•œ ๋‹จ์–ด ๋๋‚˜๋ฉด temp๋ฅผ ๋น„์›Œ์คŒ
            if (Arrays.asList(ENG).contains(temp)) {
                idx = Arrays.asList(ENG).indexOf(temp);
                result += NUM[idx];
                temp = "";
            }
        }
        return result;
    }
}

 

 

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์˜ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ–ˆ๋Š”๋ฐ ๋‚ด๊ฐ€ ๋„ˆ๋ฌด ์–ด๋ ต๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ๊ฐœ์„ ํ•ด๋ณด์•˜๋‹ค.

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฐ˜๋ณตํ•˜๋ฉฐ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ๋งŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ replace ํ•  ๊ฐœ์ˆ˜ ๋ฐ ์Œ์ด ์ •ํ•ด์ ธ์žˆ์œผ๋‹ˆ๊นŒ ๋Œ€์ฒดํ•  ๋ฌธ์ž๋“ค์„ ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์—์„œ ์ฐพ์•„๋‚ด๊ณ , ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. 

 

๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๊ณ  ์˜๋‹จ์–ด๋ฅผ ์ฐพ์•„์„œ ๋ฐ”๋กœ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค.

 

 

import java.util.*;

class Solution {
    static final String[] ENG = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    static final String[] NUM = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
    
    public int solution(String s) {
        for(int i = 0; i < 10; i++) {
            s = s.replaceAll(ENG[i], NUM[i]);
        }

        return Integer.parseInt(s);
    }
}

 

 

๋Œ“๊ธ€