Kotlin/Kotlin문제풀이

Programmers Kotlin 문제 풀이 41 ~ 50

Happy._. 2024. 5. 23. 07:12

이상한 문자 만들기

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

class Solution {
    fun solution(s: String): String {
        var index = 0
        var result = ""
        
        for (c in s) {
            if (c == ' ') {
                result += c
                index = 0
                continue
            }

            if (index % 2 == 0) {
                result += c.uppercase()
            } else {
                result += c.lowercase()
            }

            index++
        }
        
        return result
    }
}

 

삼총사

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

class Solution {
    fun solution(number: IntArray): Int {
        var count = 0
        for (i in 0 until number.size) {
            for (j in i + 1 until number.size) {
                for (k in j + 1 until number.size) {
                    if (number[i] + number[j] + number[k] == 0) {
                        count++
                    }
                }
            }
        }
        return count
    }
}

 

크기가 작은 부분 문자열

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

class Solution {
    fun solution(t: String, p: String): Int {
        var count = 0

        for (i in 0 until t.length - p.length + 1) {
            val substr = t.substring(i, i + p.length)

            if (substr.toLong() <= p.toLong()) {
                count++
            }
        }
        
        return count
    }
}

 

최소직사각형

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

class Solution {
    fun solution(sizes: Array<IntArray>): Int {
        val widthList = mutableListOf<Int>()
        val heightList = mutableListOf<Int>()

        for (arr in sizes) {
            if(arr[0] < arr[1]) {
                heightList.add(arr[0])
                widthList.add(arr[1])
            } else {
                widthList.add(arr[0])
                heightList.add(arr[1])   
            }
        }
        
        widthList.sort()
        heightList.sort()

        return heightList.last() * widthList.last()
    }
}

 

시저암호

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

import kotlin.math.abs

class Solution {
    fun solution(s: String, n: Int): String {
        val upperCaseList = listOf('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z')
        val lowerCaseList = listOf('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z')
    var result = ""

    for (c in s) {
        if (upperCaseList.indexOf(c) != -1) {
            var index = upperCaseList.indexOf(c)
            index = if (index + n > 25) abs(26 - (index + n)) else index + n
            result += upperCaseList[index]
        }

        if (lowerCaseList.indexOf(c) != -1) {
            var index = lowerCaseList.indexOf(c)
            index = if (index + n > 25) abs(26 - (index + n)) else index + n
            result += lowerCaseList[index]
        }

        if (c == ' ') result += ' '
    }

    return result
    }
}

 

숫자 문자열과 영단어

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

class Solution {
    fun solution(s: String): Int {
        val stringNumberList = listOf("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
        val numberList = listOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")

        var result = ""
        var temp = ""

        s.forEach {
            if (numberList.contains(it.toString())) {
                result += it
            } else {
                temp += it

                if(stringNumberList.contains(temp)) {
                    result += stringNumberList.indexOf(temp)
                    temp = ""
                }
            }
        }
        
        return result.toInt()
    }
}

 

문자열 내 마음대로 정렬하기

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

class Solution {
    fun solution(strings: Array<String>, n: Int): Array<String> {
        return strings.toList().sortedBy { it }.sortedBy { it[n] }.toTypedArray()
    }
}

 

K번째수

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

class Solution {
    fun solution(array: IntArray, commands: Array<IntArray>): IntArray {
        val result = mutableListOf<Int>()
        
        for (arr in commands) {
            val number = array.sliceArray(IntRange(arr[0] - 1, arr[1] - 1)).sortedArray()[arr[2] - 1]
            result.add(number)
        }
        
        return result.toIntArray()
    }
}

 

두 개 뽑아서 더하기

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

class Solution {
    fun solution(numbers: IntArray): IntArray {
        val set = mutableSetOf<Int>()

        numbers.forEachIndexed { outIndex, i ->
            numbers.forEachIndexed { inIndex, j ->
                if (outIndex != inIndex) {
                    set.add(i + j)
                }
            }
        }
        
        return set.sorted().toIntArray()
    }
}

 

가장 가까운 같은 글자

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

class Solution {
    fun solution(s: String): IntArray {
        val result = mutableListOf<Int>()
        result.add(-1)
        
        for(i in s.indices) {
            for(j in i-1 downTo 0) {
                if(s.get(i) == s.get(j)) {
                    result.add(i-j)
                    break
                }

                if(j == 0 && s.get(i) != s.get(j)) {
                    result.add(-1)
                }
            }
        }
        
        return result.toIntArray()
    }
}