Contents

์˜น์•Œ์ด 1 (with.Java)

   May 19, 2024     2 min read

โ€œ์˜น์•Œ์ด 1 (with.Java)โ€ ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉฐ, ํ’€์—ˆ๋˜ ๋ฌธ์ œ์— ๋Œ€ํ•œ ํšŒ๊ณ ์™€ ๋‹ค๋ฅธ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๋ฉฐ, ์•Œ์•„๊ฐ€๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ์— ๋Œ€ํ•ด ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฌธ์ œ

๋จธ์“ฑ์ด๋Š” ํƒœ์–ด๋‚œ ์ง€ 6๊ฐœ์›” ๋œ ์กฐ์นด๋ฅผ ๋Œ๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์กฐ์นด๋Š” ์•„์ง โ€œayaโ€, โ€œyeโ€, โ€œwooโ€, โ€œmaโ€ ๋„ค ๊ฐ€์ง€ ๋ฐœ์Œ์„ ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ ์‚ฌ์šฉํ•ด ์กฐํ•ฉํ•œ(์ด์–ด ๋ถ™์ธ) ๋ฐœ์Œ๋ฐ–์— ํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด ๋ฐฐ์—ด babbling์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, ๋จธ์“ฑ์ด์˜ ์กฐ์นด๊ฐ€ ๋ฐœ์Œํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ returnํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

์ œํ•œ์‚ฌํ•ญ

  • 1 โ‰ค babbling์˜ ๊ธธ์ด โ‰ค 100
  • 1 โ‰ค babbling[i]์˜ ๊ธธ์ด โ‰ค 15
  • babbling์˜ ๊ฐ ๋ฌธ์ž์—ด์—์„œ โ€œayaโ€, โ€œyeโ€, โ€œwooโ€, โ€œmaโ€๋Š” ๊ฐ๊ฐ ์ตœ๋Œ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ฆ‰, ๊ฐ ๋ฌธ์ž์—ด์˜ ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ค‘์—์„œ โ€œayaโ€, โ€œyeโ€, โ€œwooโ€, โ€œmaโ€๊ฐ€ ํ•œ ๋ฒˆ์”ฉ๋งŒ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค.
  • ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

babblingresult
[โ€œayaโ€, โ€œyeeโ€, โ€œuโ€, โ€œmaaโ€, โ€œwyeooโ€]1
[โ€œayayeโ€, โ€œuuumaโ€, โ€œyeโ€, โ€œyemawooโ€, โ€œayaaโ€]3

๋ฌธ์ œ์— ๋Œ€ํ•œ ๋‚˜์˜ ํ’€์ด

class Solution {
    public int solution(String[] babbling) {
        int answer = 0;
        String[] vaildBabblings = {"aya", "ye", "woo", "ma"};

        for(String sound : babbling){
            for(String validBabbling : vaildBabblings){
                sound = sound.replace(validBabbling," ");
            }

            sound = sound.replace(" ","");

            if(sound.equals("")){
                answer++;
            }
        }
        return answer;
    }
}

ํ’€์ด ๋ฆฌ๋ทฐ

๋จผ์ € ์œ ํšจํ•œ ์Œ์ ˆ ์กฐํ•ฉ์„ ์ €์žฅํ•˜๋Š” vaildBabblings ๋ฐฐ์—ด์ด ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค.

์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ๋ฌธ์ž์—ด(sound)์—์„œ ์œ ํšจํ•œ ์Œ์ ˆ ์กฐํ•ฉ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

์™ธ๋ถ€ ๋ฐ˜๋ณต๋ฌธ์€ ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด ๋ฐฐ์—ด(babbling)์„ ๋ฐ˜๋ณตํ•˜๊ณ , ๋‚ด๋ถ€ ๋ฐ˜๋ณต๋ฌธ์€ ์œ ํšจํ•œ ์Œ์ ˆ ์กฐํ•ฉ์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.

๊ฐ ์œ ํšจํ•œ ์Œ์ ˆ ์กฐํ•ฉ์„ ํ•ด๋‹น ๋ฌธ์ž์—ด์—์„œ ๊ณต๋ฐฑ์œผ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.

์œ ํšจํ•œ ์Œ์ ˆ ์กฐํ•ฉ์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•œ ํ›„์—๋Š” ๋ฌธ์ž์—ด์—์„œ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.

๊ณต๋ฐฑ์ด ๋ชจ๋‘ ์ œ๊ฑฐ๋œ ๋ฌธ์ž์—ด์ด ๋นˆ ๋ฌธ์ž์—ด(โ€œโ€œ)์ธ ๊ฒฝ์šฐ์—๋งŒ answer๋ฅผ ์ฆ๊ฐ€์‹œํ‚ต๋‹ˆ๋‹ค.

์ตœ์ข…์ ์œผ๋กœ answer๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด ์ฝ”๋“œ๋Š” ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์—์„œ ์œ ํšจํ•œ ์Œ์ ˆ ์กฐํ•ฉ์„ ์ฐพ์•„ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.