[kotlin] ํฌ๊ธฐ๊ฐ€ ์ž‘์€ ๋ถ€๋ถ„๋ฌธ์ž์—ด (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜) : substring(), windowed()
๋ฐ˜์‘ํ˜•

 

 

 

 

 

๋ฌธ์ œ

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

 

 

 


 

์„ฑ๊ณต ์ฝ”๋“œ

 

class Solution {
    fun solution(t: String, p: String): Int {
        
        var count = 0
        // p์˜ ๊ธธ์ด ๋งŒํผ, t์˜ ๋ฌธ์ž์—ด์„ ๋ฝ‘์•„์„œ ๋น„๊ต
        for (i in 0 .. t.length - p.length){
            val substring = t.substring(i, i + p.length) // i๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜์—ฌ i + p.length ์ „๊นŒ์ง€ ๋ฌธ์ž์—ด ์ž๋ฅด๊ธฐ
            
            if(substring.toLong() <= p.toLong()){
                count++
            }
        }
        return count
    }
}

 


 

์„ค๋ช…

 

  1. p์˜ ๊ธธ์ด๋งŒํผ t์˜ ๋ฌธ์ž์—ด์„ ๋ฝ‘์•„์„œ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•ด for๋ฌธ์„ ๋งŒ๋“ ๋‹ค.
    for๋ฌธ์€ 0๋ถ€ํ„ฐ (์ด ๊ธธ์ด - p์˜ ๊ธธ์ด)๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.
    (substring(i, i + p.length)๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ด๋ ‡๊ฒŒ ๋ฒ”์œ„๋ฅผ ์„ค์ •)
  2. ๋ฝ‘์•„๋‚ธ ๋ฌธ์ž์—ด๋งˆ๋‹ค p์™€ ํฌ๊ธฐ๋ฅผ ๋น„๊ตํ•œ๋‹ค.
    p๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉด count๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ฆฐ๋‹ค.
  3. ๋ชจ๋“  ๋น„๊ต๋ฅผ ์™„๋ฃŒํ•˜๋ฉด count๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

 

 

๐Ÿ’ก substring()

val str = "Hello, world!"

val result1 = str.substring(7) // "world!"
val result2 = str.substring(7, 12) // "world"
val result3 = str.substring(7..11) // "world"
  • ๋ฌธ์ž์—ด์—์„œ ํŠน์ • ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ถ”์ถœํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • substring(startIndex: Int): String: ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์˜ ๋๊นŒ์ง€ ์ถ”์ถœ
  • substring(startIndex: Int, endIndex: Int): String: ์‹œ์ž‘ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ๋ ์ธ๋ฑ์Šค๊นŒ์ง€ ์ถ”์ถœ
  • substring(range: IntRange): String: ์ง€์ •๋œ ๋ฒ”์œ„์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ถ”์ถœ

 

 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(t: String, p: String): Int {
        return (0..t.length - p.length)
            .map{ t.substring(it until it + p.length) }
            .count { it <= p }
    }
}
  • map์„ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ”๋กœ ๋ฐ˜ํ™˜ํ•˜์—ฌ count๋ฅผ ์„ธ์„œ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ค„์ธ ํ’€์ด๋‹ค.

 

๐Ÿ’กmap

val numbers = listOf(1, 2, 3, 4, 5)
val squaredNumbers = numbers.map { it * it }
println(squaredNumbers) // ์ถœ๋ ฅ: [1, 4, 9, 16, 25]
  • ๊ฐ ์š”์†Œ๋ฅผ ์ฃผ์–ด์ง„ ๋žŒ๋‹ค ํ•จ์ˆ˜์— ์ ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑํ•œ๋‹ค.
  • ๊ธฐ์กด ์ปฌ๋ ‰์…˜์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ๋žŒ๋‹ค ํ•จ์ˆ˜๊ฐ€ ์ ์šฉ๋œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์€ ์ƒˆ๋กœ์šด ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

class Solution {
    fun solution(t: String, p: String): Int =
        t.windowed(p.length).count {
            it.toLong() <= p.toLong()
        }
}
  • windowed()๋ฅผ ์ด์šฉํ•œ ํ’€์ด๋‹ค. ์œˆ๋„์šฐ์˜ ์‚ฌ์ด์ฆˆ๋งŒ p.length๋กœ ์„ค์ •๋˜์–ด step์€ ๊ธฐ๋ณธ๊ฐ’์ธ 1๋กœ ์„ค์ •๋˜์—ˆ๋‹ค.

 

 

๐Ÿ’กwindowed()

fun <T> Iterable<T>.windowed(
    size: Int,
    step: Int = 1,
    partialWindows: Boolean = false
): List<List<T>>
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

// ์œˆ๋„์šฐ ํฌ๊ธฐ๊ฐ€ 3์ด๊ณ , ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ธ ๊ฒฝ์šฐ
val result1 = list.windowed(size = 3, step = 1)
// ๊ฒฐ๊ณผ: [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]

// ์œˆ๋„์šฐ ํฌ๊ธฐ๊ฐ€ 4์ด๊ณ , ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ 2์ธ ๊ฒฝ์šฐ
val result2 = list.windowed(size = 4, step = 2)
// ๊ฒฐ๊ณผ: [[1, 2, 3, 4], [3, 4, 5, 6], [5, 6, 7, 8], [7, 8, 9, 10]]

// ์œˆ๋„์šฐ ํฌ๊ธฐ๊ฐ€ 2์ด๊ณ , ์ด๋™ ๊ฑฐ๋ฆฌ๊ฐ€ 3์ธ ๊ฒฝ์šฐ
val result3 = list.windowed(size = 2, step = 3)
// ๊ฒฐ๊ณผ: [[1, 2], [4, 5], [7, 8], [10]]

// ๋ถ€๋ถ„ ์œˆ๋„์šฐ ํฌํ•จ
val result4 = list.windowed(size = 3, step = 2, partialWindows = true)
// ๊ฒฐ๊ณผ: [[1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]]

// ์œˆ๋„์šฐ ํฌ๊ธฐ๋งŒ ์„ค์ •, ์ด๋™ ๊ฑฐ๋ฆฌ๋Š” ๊ธฐ๋ณธ 1๋กœ ์„ค์ •๋จ
val result5 = list.windowed(3)
// ๊ฒฐ๊ณผ : [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]
  • ์ฃผ์–ด์ง„ ํฌ๊ธฐ์˜ ์ฐฝ์„ ์ด๋™ํ•˜๋ฉด์„œ ์ปฌ๋ ‰์…˜์„ ๋ถ„ํ• ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ
  • ๋ฐ˜ํ™˜ ๊ฐ’์€ ์ฃผ์–ด์ง„ ํฌ๊ธฐ์˜ ๋ถ€๋ถ„ ์ปฌ๋ ‰์…˜ ๋˜๋Š” ์œˆ๋„์šฐ
  • size: ์ƒ์„ฑํ•  ๊ฐ ์œˆ๋„์šฐ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’
  • step: ์œˆ๋„์šฐ๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ๊ฐ„๊ฒฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’. ๊ธฐ๋ณธ๊ฐ’์€ 1์ด๋ฉฐ, ์ด๋Š” ํ•œ ๋ฒˆ์— ํ•œ ์š”์†Œ์”ฉ ์ด๋™ํ•จ์„ ์˜๋ฏธ
  • partialWindows: ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„ ์œˆ๋„์šฐ์˜ ํฌ๊ธฐ๊ฐ€ size๋ณด๋‹ค ์ž‘์„ ๋•Œ, ์ด ๋ถ€๋ถ„ ์œˆ๋„์šฐ๋ฅผ ํฌํ•จํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’. ๊ธฐ๋ณธ๊ฐ’์€ false์ด๋ฉฐ, ์ด ๊ฒฝ์šฐ ๋ถ€๋ถ„ ์œˆ๋„์šฐ๋Š” ํฌํ•จ๋˜์ง€ ์•Š์Œ

 

 

 

์ •ํ™•ํžˆ ๊ธฐ์–ต์€ ์•ˆ๋‚˜์ง€๋งŒ, ์ด ์ด์™ธ์—๋„ ํŒ€์›๋ถ„์ด ๊ณต์œ ํ•ด์ฃผ์‹  ์ฝ”๋“œ ์ค‘์— chucked๋ฅผ ์ด์šฉํ•˜์‹  ์ฝ”๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค.
๊ฒฐ๊ตญ์—” ์ŠคํŠธ๋ง ๊ฐ’์œผ๋กœ p์™€ ๋น„๊ตํ•˜๊ฒŒ ๋˜๋ฉด์„œ toInt()๋‚˜ toLong()๊ณผ ๊ฐ™์€ ํ˜•๋ณ€ํ™˜์„ ์‹ ๊ฒฝ์“ธ ํ•„์š”๊ฐ€ ์—†๋Š” ์ฝ”๋“œ์˜€๋‹ค.

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T