SW Test/Programmers

[Programmers] Lv2.JadenCase ๋ฌธ์ž์—ด ๋งŒ๋“ค๊ธฐ(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 9. 12. 11:07
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/12951#

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/12951#

 

 

 

ํ’€์ด

  • ๋จผ์ €, ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ์ œ์™ธํ•˜๊ณ  ๋‹ค๋ฅธ ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ์ „์ฒด ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์žํ™” ์‹œํ‚ต๋‹ˆ๋‹ค.(toLowerCase)
  • ์ดํ›„ " " delimeter ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•œ ๋’ค ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์žํ™” ์‹œํ‚ค๋ฉด ๋ฉ๋‹ˆ๋‹ค(toUpperCase). ํ•˜์ง€๋งŒ delimeter๊ฐ€ ์—ฐ์†์ ์œผ๋กœ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ณ  ์—ฐ์†์ ์ผ ๊ฒฝ์šฐ ๋นˆ๋ฌธ์ž์—ด์ด split ๋ฆฌ์ŠคํŠธ๋กœ ์ถ”๊ฐ€๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿด ๋•Œ first๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ firstOrNull ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ Null Check ํ›„ ๋Œ€๋ฌธ์žํ™” ์‹œํ‚ค๋„๋ก ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.
class Solution {
    fun solution(s: String): String {
        return s.toLowerCase().split(" ").map{
            it.firstOrNull()?.let{ first -> 
                it.replaceFirst(first, first.toUpperCase())
            } ?: it
        }.joinToString(" ")
    }
}

 

 

 

์ฐธ๊ณ 

 

toLowerCase - Kotlin Programming Language

 

kotlinlang.org

 

toUpperCase - Kotlin Programming Language

 

kotlinlang.org

 

replaceFirst - Kotlin Programming Language

 

kotlinlang.org

 

firstOrNull - Kotlin Programming Language

 

kotlinlang.org

 

๋ฐ˜์‘ํ˜•