Publish:

ํƒœ๊ทธ: , , , , ,

์นดํ…Œ๊ณ ๋ฆฌ:

img_3.png

๋ฌธ์ œ

๋ฌธ์ œ ๋งํฌ

img_3.png

ํ’€์ด

์ฒซ๋ฒˆ์งธ ํ’€์ด (์˜ค๋‹ต)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
    public String solution(String s) {
        String lcString = s.toLowerCase();
        String[] split = lcString.split(" ");
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < split.length; i++) {
          String string = split[i];
          if (!string.isEmpty()) {
            String front = string.substring(0, 1).toUpperCase();
            result.append(!front.isEmpty() ? front : "");
            result.append(split[i], 1, split[i].length());
            result.append(i < split.length - 1 ? " " : "");
          }
        }
        return result.toString();
    }
}

์ฒซ๋ฒˆ์งธ ํ’€์ด์—์„œ ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ๋˜ ๋ถ€๋ถ„์€ ๋จผ์ € result.append(!front.isEmpty() ? front : ""); ์ด ์ฝ”๋“œ๋Š” front ๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ด ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ตณ์ด ์กฐ๊ฑด ๊ฒ€์‚ฌ๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋˜ํ•œ result.append(i < split.length - 1 ? " " : ""); ์ด ๋ถ€๋ถ„์ด if ๋ฌธ ์•ˆ์— ๋“ค์–ด๊ฐ€ ์žˆ์–ด์„œ ๊ณต๋ฐฑ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด์„œ๋Š” ์ž˜๋ชป๋œ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

1
2
3
4
5
6
7
8
// input
   "  hello world  "

// s.split(" ")
[, , , ", , hello, world, , "]            // " " ๊ณต๋ฐฑ ๋ฌธ์ž์—ด์„ split(" ") ํ•˜๊ฒŒ๋˜๋ฉด ""

// output
" Hello World "

์œ„์—์„œ s.split(โ€œ โ€œ) ๋ฐฐ์—ด์˜ ๋งจ ์•ž ๋นˆ ๋ฌธ์ž์—ด(โ€œโ€œ)์— ๋Œ€ํ•ด์„  if ๋ฌธ์ด false ๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜์–ด result ์— ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ณต๋ฐฑ๋„ ์ถ”๊ฐ€ํ•ด์„œ ๋ฐ˜ํ™˜ ํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ œ์ด๋ฏ€๋กœ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ์˜ค๋ฅ˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค.

์œ„ ์˜ˆ์ œ์—์„œ์ฒ˜๋Ÿผ split(โ€œ โ€œ) ๋กœ ์ž๋ฅด๊ฒŒ ๋˜๋ฉด ๋’ค์ชฝ ๊ณต๋ฐฑ์€ ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค. ๋งจ ๋’ค์— ๋นˆ ๋ฌธ์ž์—ด๋„ ์ถœ๋ ฅ๊ฒฐ๊ณผ์— ํฌํ•จํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์—” split(" ", -1) ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

1
public String[] split(String regex, int limit) {

split() ํ•จ์ˆ˜๋Š” ๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ ๊ฐ€์ง„ ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๋Š”๋ฐ, limit ์ด ์–‘์ˆ˜์ธ์ง€, 0์ธ์ง€, ์Œ์ˆ˜์ธ์ง€์— ๋”ฐ๋ผ ํŒจํ„ด์ด ์ ์šฉ๋˜๋Š” ํšŸ์ˆ˜๋ฅผ ์ œ์–ดํ•˜๋ฉฐ, ๋”ฐ๋ผ์„œ ๊ฒฐ๊ณผ ๋ฐฐ์—ด์˜ ๊ธธ์ด์— ์˜ํ–ฅ์„ ๋ฏธ์นœ๋‹ค.

img_4.png split()

case ํŒจํ„ด ์ ์šฉ ํšŸ์ˆ˜ ๋ฐฐ์—ด ๊ธธ์ด
์–‘์ˆ˜ limit - 1 <= limit
0 ์ตœ๋Œ€ ๋งˆ์ง€๋ง‰ ๋นˆ ๋ฌธ์ž์—ด ๋ฒ„๋ฆผ
์Œ์ˆ˜ ์ตœ๋Œ€ ๋งˆ์ง€๋ง‰ ๋นˆ ๋ฌธ์ž์—ด ํฌํ•จ

๋‘๋ฒˆ์งธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
    public String solution(String s) {
        String lcString = s.toLowerCase();
        String[] split = lcString.split(" ", -1);
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < split.length; i++) {
          String string = split[i];
          if (!string.isEmpty()) {
            String front = string.substring(0, 1).toUpperCase();
            result.append(front);
            result.append(string.substring(1));
          }
          // 
          if (i < split.length - 1) {
            result.append(" ");
          }
        }
        return result.toString();
    }
}

์ „์ฒด์ ์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ์‚ผํ•ญ์—ฐ์‚ฐ์„ ์—†์• ๊ณ  ๋’ค์— ๊ณต๋ฐฑ์ด ํฌํ•จ๋œ ์ƒํƒœ๋กœ ์ž…๋ ฅ ๋˜๋Š” ๋ฌธ์ž์—ด(" hello world ")์— ๋Œ€ํ•ด์„œ๋„ ๊ทธ๋Œ€๋กœ ํฌํ•จํ•ด์„œ ์ถœ๋ ฅํ•˜๋„๋ก ์ˆ˜์ •ํ–ˆ๋‹ค.

๋ฐฉ๋ฌธํ•ด ์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Œ“๊ธ€,์ง€์ ,ํ”ผ๋“œ๋ฐฑ ์–ธ์ œ๋‚˜ ํ™˜์˜ํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ