Contents

๋ชจ์Œ ์ œ๊ฑฐ(with. Java)

   Feb 14, 2024     1 min read

โ€œ๋ชจ์Œ ์ œ๊ฑฐโ€ ๋ฌธ์ œ์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณธ ๊ธ€์ž…๋‹ˆ๋‹ค.

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

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

๋ฌธ์ œ

์˜์–ด์—์„  a, e, i, o, u ๋‹ค์„ฏ ๊ฐ€์ง€ ์•ŒํŒŒ๋ฒณ์„ ๋ชจ์Œ์œผ๋กœ ๋ถ„๋ฅ˜ํ•ฉ๋‹ˆ๋‹ค.

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

์ œํ•œ์‚ฌํ•ญ

  • my_string์€ ์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์Šต๋‹ˆ๋‹ค.
  • 1 โ‰ค my_string์˜ ๊ธธ์ด โ‰ค 1,000

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

my_stringresult
โ€œbusโ€โ€œbsโ€
โ€œnice to meet youโ€โ€œnc t mt yโ€

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

class Solution {
    public String solution(String my_string) {
        StringBuilder answer = new StringBuilder();
        char[] arrString = my_string.toCharArray();

        for(char charString : arrString){
            if(charString != 'a' && charString != 'e' && charString != 'i' && charString != 'o' && charString != 'u'){
                answer.append(charString);
            }
        }
        return answer.toString();
    }
}

ํ’€์ด ์„ค๋ช…

solution(String my_string): ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด my_string์—์„œ ๋ชจ์Œ์„ ์ œ์™ธํ•œ ๋ฌธ์ž๋“ค์„ ์ถ”์ถœํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ํšจ์œจ์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ž์—ด์„ ๋ฌธ์ž ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ๋ชจ์Œ์ธ์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ ๋ชจ์Œ์ด ์•„๋‹Œ ๊ฒฝ์šฐ StringBuilder์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

์ตœ์ข…์ ์œผ๋กœ StringBuilder๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ ์žฅ์ 

  • ํšจ์œจ์ ์ธ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ: StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  • ๊ฐ„๊ฒฐํ•œ ๊ตฌํ˜„: ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•œ ๋ชจ์Œ ๊ฒ€์‚ฌ๋ฅผ ํ†ตํ•ด ๊ฐ„๊ฒฐํ•œ ๊ตฌํ˜„์„ ์ œ๊ณตํ•œ๋‹ค.

์ฝ”๋“œ ๋‹จ์ 

  • ํ•˜๋“œ์ฝ”๋”ฉ๋œ ๋ชจ์Œ ๋ฆฌ์ŠคํŠธ: ๋ชจ์Œ์„ ๊ฒ€์‚ฌํ•  ๋•Œ โ€˜aโ€™, โ€˜eโ€™, โ€˜iโ€™, โ€˜oโ€™, โ€˜uโ€™๋ฅผ ํ•˜๋“œ์ฝ”๋”ฉํ•˜์—ฌ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์–ด, ๋‚˜์ค‘์— ๋ชจ์Œ์ด ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค.