https://programmers.co.kr/learn/courses/30/lessons/60057
์ฝ๋ฉํ ์คํธ ์ฐ์ต - ๋ฌธ์์ด ์์ถ
๋ฐ์ดํฐ ์ฒ๋ฆฌ ์ ๋ฌธ๊ฐ๊ฐ ๋๊ณ ์ถ์ ์ดํผ์น๋ ๋ฌธ์์ด์ ์์ถํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๊ณต๋ถ๋ฅผ ํ๊ณ ์์ต๋๋ค. ์ต๊ทผ์ ๋๋์ ๋ฐ์ดํฐ ์ฒ๋ฆฌ๋ฅผ ์ํ ๊ฐ๋จํ ๋น์์ค ์์ถ ๋ฐฉ๋ฒ์ ๋ํด ๊ณต๋ถ๋ฅผ ํ๊ณ ์๋๋ฐ, ๋ฌธ์
programmers.co.kr
์ด์
์ ์ถ ํ ์คํธ 5๋ฒ๋ง ์คํจ
ํด๊ฒฐ
import kotlin.test.*
class Solution {
fun solution(s : String) : Int {
var result = Int.MAX_VALUE
if(s.length == 1) result = 1
for(unitCount in 1 .. (s.count() - 1)){
val zipResult = zipString(unitCount, s)
if(result > zipResult) result = zipResult
}
return result
}
fun zipString(unitCount: Int, s: String) : Int {
var result = 0
var zipCount = 1
var prevUnit = ""
for (i in 0..(s.count()) step unitCount) {
var currentUnit = getUnit(i, unitCount, s)
if(prevUnit == currentUnit) {
zipCount++
} else {
if(zipCount > 1){
result += (zipCount.toString().length + currentUnit.length)
} else {
result += currentUnit.length
}
zipCount = 1
prevUnit = currentUnit
}
}
return result
}
fun getUnit(index: Int, unitCount: Int, s: String) : String {
return if(index + unitCount > s.count()) {
return s.subSequence(index, s.count()).toString()
} else {
s.subSequence(index, index + unitCount).toString()
}
}
}
class SolutionTest {
val solution = Solution()
fun testGetUnit() {
val testCase1 = Pair(Triple(3, 3, "ababcdcdababcdcd"), "bcd")
val testCase2 = Pair(Triple(0, 4, "ababcdcdababcdcd"), "abab")
val testCase3 = Pair(Triple(8, 8, "ababcdcdababcdcd"), "ababcdcd")
val testCase4 = Pair(Triple(6, 6, "abcabcdede"), "dede")
val testCase5 = Pair(Triple(0, 3, "abcabcdede"), "abc")
val testCases = listOf(testCase1 , testCase2, testCase3, testCase4, testCase5)
for (test in testCases) {
val (quest, answer) = test
val (index, unitCount, s) = quest
val result = solution.getUnit(index, unitCount, s.trim())
assertEquals(answer, result, "epected : $answer, actural : $result")
}
}
fun testZipString() {
val testCase1 = Triple("ababcdcdababcdcd", 1, "ababcdcdababcdcd")
val testCase2 = Triple("ababcdcdababcdcd", 2, "2ab2cd2ab2cd")
val testCase3 = Triple("ababcdcdababcdcd",8, "2ababcdcd")
val testCase4 = Triple("abcabcdede", 2, "abcabc2de")
val testCase5 = Triple("abcabcdede",3, "2abcdede")
val testCases = listOf(testCase1 , testCase2, testCase3, testCase4, testCase5)
for (test in testCases) {
val (s, unitCount, answer) = test
val result = solution.zipString(unitCount, s)
assertEquals(answer.length, result, "epected : ${answer.length}, actural : $result")
}
}
fun testSolution() {
val testCase1 = Pair("aabbaccc", 7)
val testCase2 = Pair("ababcdcdababcdcd",9)
val testCase3 = Pair("abcabcdede",8)
val testCase4 = Pair("abcabcabcabcdededededede",14)
val testCase5 = Pair("xababcdcdababcdcd",17)
val testCases = listOf(testCase1 , testCase2, testCase3, testCase4, testCase5)
for(test in testCases) {
val (string, answer) = test
val result = solution.solution(string)
assertEquals(answer, result, "epected : $answer, actural : $result")
}
}
}
fun main() {
val solutionTest = SolutionTest()
solutionTest.testGetUnit()
solutionTest.testZipString()
solutionTest.testSolution()
}
5๋ฒ๋ฌธ์ ๋ ์ ๋ ฅ ์คํธ๋ง์ด 1๊ธ์์ธ ๊ฒฝ์ฐ ํ ์คํธ๋ก ์ถ์ธก
8๋ฒ์งธ์ค์ ํ๊ธ์์ธ ๊ฒฝ์ฐ ์์ธ์ฒ๋ฆฌ๋ก ํ ์คํธ์ ํต๊ณผํ ์ ์์๋ค