๐Ÿ’ป๋ฌธ์ œ ํ’€์ด - [Java] 1152. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜

์ดํ•˜์–€ยท2024๋…„ 1์›” 31์ผ
0

๐Ÿฃ ๋ฐฑ์ค€

๋ชฉ๋ก ๋ณด๊ธฐ
1/33
post-thumbnail

๐Ÿ“๋ฌธ์ œ ์ถœ์ฒ˜ : https://www.acmicpc.net/problem/1152
๐Ÿ“code ์ €์žฅ์†Œ : hayannn/CodingTest_Java_NKLCBHub/#1152 ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜


๐Ÿค” ๋ฌธ์ œ ์š”์•ฝ

์˜์–ด ๋Œ€์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๊ณ , ๊ทธ ์†์—์„œ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

๐Ÿ“ ์˜ˆ์ œ ์ž…๋ ฅ ๋ถ„์„

  • Case 1 : ๋‹จ์–ด ์‚ฌ์ด์˜ ๊ณต๋ฐฑ๋งŒ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
  • Case 2 : ๋ฌธ์ž์—ด ๋งจ ์•ž ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ
  • Case 3 : ๋ฌธ์ž์—ด ๋งจ ๋’ค ๊ณต๋ฐฑ์„ ํฌํ•จํ•˜๋Š” ๊ฒฝ์šฐ

๐Ÿšจ ์ฃผ์˜์‚ฌํ•ญ

  • ๋‹จ์–ด์˜ ๊ธฐ์ค€์€ ๊ณต๋ฐฑ์ด๋ฉฐ, ๋ฌธ์ž์—ด์˜ ๋งจ ์•ž๊ณผ ๋งจ ๋’ค์—๋„ ๊ณต๋ฐฑ์ด ์˜ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ํŠน์ด ์‚ฌํ•ญ์ด ์žˆ์Œ์— ์ฃผ์˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.



1๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1 - Split + trim

๐Ÿ“๊ณต๋ถ€ํ•œ ๋‚ด์šฉ - [Java] ๋ฐฑ์ค€ 1152. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜#Split

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

  • ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ
  • if) ๋ฌธ์ž์—ด์— ๊ณต๋ฐฑ๋งŒ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ -> 0 ๋ฐ˜ํ™˜
  • else) split.length๋กœ ๋‹จ์–ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ
import java.util.Scanner;

public class BJ1152 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String string = scanner.nextLine().trim(); //๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ

        if(string.isEmpty()) // ๋ฌธ์ž์—ด์— ๊ณต๋ฐฑ๋งŒ ์ž…๋ ฅ๋œ ๊ฒฝ์šฐ
            System.out.println('0'); // 0 ๋ฐ˜ํ™˜
        else
            System.out.println(string.split(" ", -1).length); //๋‹จ์–ด ๊ฐœ์ˆ˜ ์ถœ๋ ฅ

    }
}




2๏ธโƒฃ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 2 - StringTokenizer

๐Ÿ“๊ณต๋ถ€ํ•œ ๋‚ด์šฉ - [Java] ๋ฐฑ์ค€ 1152. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜#stringtokenizer

๐Ÿ“ ํ’€์ด ๊ณผ์ • ๋ฐ ์ฝ”๋“œ

  • ๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ
  • stringTokenizer๋ฅผ ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์„ ์–ธ
  • Token ๊ฐœ์ˆ˜๋ฅผ ์„ธ์„œ ์ถœ๋ ฅ CountTokens()
import java.util.Scanner;
import java.util.StringTokenizer;

public class BJ1152TWO {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        String string = scanner.nextLine(); //๋ฌธ์ž์—ด ์ž…๋ ฅ ๋ฐ›๊ธฐ

        StringTokenizer stringTokenizer = new StringTokenizer(string, " "); //stringTokenizer๋ฅผ ๊ณต๋ฐฑ ๊ธฐ์ค€์œผ๋กœ ์„ ์–ธ
        System.out.println(stringTokenizer.countTokens()); // Token ๊ฐœ์ˆ˜ ์ถœ๋ ฅ
    }
}



๐Ÿ–จ๏ธ ๊ฒฐ๊ณผ ๋น„๊ต ๋ฐ ๋ถ„์„

๐Ÿ“๊ณต๋ถ€ํ•œ ๋‚ด์šฉ - [Java] ๋ฐฑ์ค€ 1152. ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜#split vs stringtokenizer

Split + trim

  • ๋ฉ”๋ชจ๋ฆฌ : 38724KB
  • ์‹œ๊ฐ„ : 528ms
  • ์ฝ”๋“œ ๊ธธ์ด : 336B

StringTokenizer

  • ๋ฉ”๋ชจ๋ฆฌ : 30140KB
  • ์‹œ๊ฐ„ : 472ms
  • ์ฝ”๋“œ ๊ธธ์ด : 324B
  • ๋ฐ์ดํ„ฐ ์–‘์ด ์ ์„ ๊ฒฝ์šฐ๋ฅผ ์ „์ œ๋กœ ํ•  ๊ฒฝ์šฐ
    โžก๏ธ ์„ฑ๋Šฅ : StringTokenizer > Split

    • Split+trim : ๋ฐ์ดํ„ฐ๋ฅผ ํ† ํฐ์œผ๋กœ ์ž˜๋ผ๋‚ธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๋Š๋ฆฝ๋‹ˆ๋‹ค.
    • StringTokenizer : ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๋กœ ์ž˜๋ผ์„œ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ๋น ๋ฆ…๋‹ˆ๋‹ค.
  • ๋‹จ, ๋ฐ์ดํ„ฐ์˜ ์–‘์ด ๋งŽ์•„์ง„๋‹ค๋ฉด ๋‘ ๊ฐ€์ง€์˜ ์„ฑ๋Šฅ์€ ํฐ ์ฐจ์ด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

profile
์–ธ์  ๊ฐ€ ๋‚ด ์ฝ”๋“œ๋กœ ์„ธ์ƒ์— ๊ธฐ์—ฌํ•  ์ˆ˜ ์žˆ๋„๋ก, BE&Data Science ๊ฐœ๋ฐœ ๊ธฐ๋ก ๋…ธํŠธโ˜˜๏ธ

0๊ฐœ์˜ ๋Œ“๊ธ€