์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋ฌธ์žฅ ์† ๋‹จ์–ด

2023. 10. 4. 14:37ใ†์ธํ”„๋Ÿฐ/์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ์ž…๋ฌธ : ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ๋Œ€๋น„

728x90

https://hyejin.tistory.com/1198

 

์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ. ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋Œ€์†Œ๋ฌธ์ž ๋ณ€ํ™˜

https://hyejin.tistory.com/1197 ์ž๋ฐ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ์ž…๋ฌธ : ์„น์…˜1. String (๋ฌธ์ž์—ด) : ๋ฌธ์ž ์ฐพ๊ธฐ https://www.inflearn.com/course/%EC%9E%90%EB%B0%94-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-%EB%AC%B8%EC%A0%9C%ED%92%80%EC%9D%B4-%EC%BD%94%ED

hyejin.tistory.com

-> ์ด์ „ ๋ฌธ์ œ ํ’€์ด 

 

3. ๋ฌธ์žฅ ์† ๋‹จ์–ด 

์„ค๋ช…

ํ•œ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ฃผ์–ด์ง€๋ฉด ๊ทธ ๋ฌธ์žฅ ์†์—์„œ ๊ฐ€์žฅ ๊ธด ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๋ฌธ์žฅ์†์˜ ๊ฐ ๋‹จ์–ด๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋ฉ๋‹ˆ๋‹ค.

 

์ž…๋ ฅ

์ฒซ ์ค„์— ๊ธธ์ด๊ฐ€ 100์„ ๋„˜์ง€ ์•Š๋Š” ํ•œ ๊ฐœ์˜ ๋ฌธ์žฅ์ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๋ฌธ์žฅ์€ ์˜์–ด ์•ŒํŒŒ๋ฒณ์œผ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ ์ค„์— ๊ฐ€์žฅ ๊ธด ๋‹จ์–ด๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ€์žฅ ๊ธธ์ด๊ฐ€ ๊ธด ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ ๋ฌธ์žฅ์†์—์„œ ๊ฐ€์žฅ ์•ž์ชฝ์— ์œ„์น˜ํ•œ

๋‹จ์–ด๋ฅผ ๋‹ต์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค.

 

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

it is time to study

 

์˜ˆ์‹œ ์ถœ๋ ฅ 1

study

 

 

๋ฌธ์ œ ํ’€์ด 1 

public String solution(String input) {
    String answer = "";
    String[] split = input.split(" ");

    for (String word : split) {
        if (word.length() > answer.length()) {
            answer = word;
        }
    }

    return answer;
}

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : ๋‚˜๋Š” ์ด ๋ฌธ์ œ๋ฅผ ํ’€ ๋•Œ split ์ด ๋– ์˜ฌ๋ผ์„œ ๊ณต๋ฐฑ๋‹จ์œ„๋กœ ๋ฌธ์ž์—ด์„ ์ชผ๊ฐœ๊ณ , ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ answer์˜ ๊ธธ์ด๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์— answer์— ๊ทธ ๋‹จ์–ด๋ฅผ ์ €์žฅํ•ด์คฌ๋‹ค. 

(์ด๋•Œ ๊ธด ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์ผ ๊ฒฝ์šฐ์— ๊ฐ€์žฅ ์•ž์ชฝ์— ์œ„์น˜ํ•œ ๋‹จ์–ด๋ฅผ ๋‹ต์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—  >= ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋‹ค๋กœ  ๋น„๊ตํ•˜๋ฉด ๋งจ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๊ฐ€ ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ™๋‹ค ๊ธฐํ˜ธ๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ์•ˆ๋œ๋‹ค.)

 

 

๋ฌธ์ œ ํ’€์ด 2

public String solution2(String input) {
    String answer = "";
    int m = Integer.MIN_VALUE; // ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”
    String[] s = input.split(" ");
    for (String x : s) {
        int len = x.length();
        if (len > m) {
            m = len;
            answer = x;
        }
    }

    return answer;
}

๐Ÿ‘พ : ๊ฐ•์‚ฌ๋‹˜์€ ๋‘๊ฐœ์˜ ํ’€์ด ๋ฐฉ๋ฒ•์„ ์ œ์‹œํ–ˆ๋Š”๋ฐ ์šฐ์„  ์ฒซ๋ฒˆ์งธ ํ’€์ด๋กœ๋Š” ๋‚˜์™€ ๋™์ผํ•˜๊ฒŒ split์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด์—ˆ๋‹ค. 

๋”ฐ๋กœ ๋‹จ์–ด ๊ธธ์ด๋ฅผ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•œ ๋‹ค์Œ์— ๊ทธ ๊ธธ์ด๋ฅผ ๋น„๊ตํ•ด์„œ answer์˜ ๊ฐ’์„ ์ €์žฅํ–ˆ๋‹ค. 

 

 

๋ฌธ์ œ ํ’€์ด 3

public String solution3(String input) {
    String answer = "";
    int m = Integer.MIN_VALUE, pos;
    while ((pos = input.indexOf(' ')) != -1) {
        String tmp = input.substring(0, pos);
        int len = tmp.length();
        if (len > m) {
            m = len;
            answer = tmp;
        }
        input = input.substring(pos + 1);
    }
    if (input.length() > m ) answer = input; // ๋งˆ์ง€๋ง‰ ๋‹จ์–ด ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•จ !!

    return answer;
}

๐Ÿ‘พ : ๋‹ค์Œ์€ indexOf์™€ subString์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. indexOf(' ')์„ ํ†ตํ•ด ๊ณต๋ฐฑ์ด ์žˆ๊ธฐ ์ „๊นŒ์ง€์˜ ๋ฌธ์ž์—ด ์œ„์น˜๋ฅผ ๊ตฌํ•˜๊ณ  ๊ทธ ๊ฐ’์ด -1์ด ์•„๋‹๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. (-1์ด๋ผ๋Š”๊ฑด ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์— ๊ณต๋ฐฑ์ด ๋” ์ด์ƒ ์—†๋‹ค๋Š” ๋œป!) 

๊ทธ ๋‹ค์Œ ๋ฌธ์ž์—ด์— subString์„ ํ†ตํ•ด ๊ณต๋ฐฑ ์ „๊นŒ์ง€ ์ชผ๊ฐœ์„œ ๊ณต๋ฐฑ ์ „ ๋ฌธ์ž๋งŒ ๊ตฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹จ์–ด์˜ ๊ธธ์ด๊ฐ€ ํ˜„์žฌ ์ €์žฅ๋œ answer์˜ ๋ฌธ์ž์—ด ๊ธธ์ด๋ณด๋‹ค ๊ธด ๊ฒฝ์šฐ์—” answer์— ๊ทธ ๋‹จ์–ด๋กœ ์ €์žฅํ•˜๊ณ , ๋ฌธ์ž์—ด ๊ธธ์ด ๋ณ€์ˆ˜ m์—๋„ ํ•ด๋‹น ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. 

 

โ—๏ธ ๊ทธ๋ฆฌ๊ณ  ์—ฌ๊ธฐ์„œ ๋๋‚˜๋ฉด ๋‹ต์ด ์•ˆ๋‚˜์˜จ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ!! 

๋ฐ˜๋ณต๋ฌธ์€ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๋Š” ํฌํ•จํ•˜์ง€ ๋ชปํ•˜๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” input.indexOf(' ')์—์„œ -1์ด ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๋Š” ๋”์ด์ƒ ๊ณต๋ฐฑ์ด ์—†๊ธฐ ๋•Œ๋ฌธ!! ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ ๋‹จ์–ด๋งŒ ๋‚จ์€ input ๋ฌธ์ž์—ด์ด answer ์— ์ €์žฅ๋œ ๊ธธ์ด๋ณด๋‹ค ํด ๊ฒฝ์šฐ์—๋Š” answer์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. 

 

๋งˆ์ง€๋ง‰ ๋‹จ์–ด๊นŒ์ง€ ์ฒ˜๋ฆฌํ•ด์ค€ ๋‹ค์Œ์— answer๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป : indexOf์™€ subString์„ ์‚ฌ์šฉํ•  ์ƒ๊ฐ์€ ์ „ํ˜€ ๋ชปํ–ˆ๋Š๋ฐ.. IndexOf ์•ž์œผ๋กœ ๊ธฐ์–ตํ•ด๋’€๋‹ค๊ฐ€ ์จ๋จน์–ด์•ผ๊ฒ ๋‹ค ใ…Ž ใ…Ž 

 

 

 

 

 

 

728x90