SW Test/Programmers
[Programmers] Lv1.μ κ· μμ΄λ μΆμ²(kotlin)
An effort will never betray π
2022. 7. 18. 22:43
λ°μν
λ¬Έμ
μμ
νμ΄
- μ΄ λ¬Έμ λ λ©μλ 체μ΄λ λ°©μμΌλ‘ μ¬λ¬ λ²μ λ©μλλ₯Ό νΈμΆνλ©΄ λΉ λ₯΄κ² ν μ μμ΅λλ€. νμ§λ§ κ·Έλ§νΌ λ€μν ν¨μμ λν΄μ μμ§ λͺ»νλ©΄ νλμ© λ‘μ§μ μμ±νλ μλ°μ μμ΅λλ€.
- ν΄λΉ νμ΄λ λ¬Έμ μμ μꡬνλ λλ‘ νλμ© λ§μΆ°κ°λ©΄μ ν μ μμ΅λλ€.
class Solution {
fun solution(new_id: String): String {
var answer: String = ""
val strbuilder = StringBuilder()
new_id.lowercase().forEach{
if(it.isLetter() || it.isDigit() || it in arrayOf('-','_','.')){
if(!(it == '.' && strbuilder.isNotEmpty() && strbuilder.get(strbuilder.length - 1) == '.')){
strbuilder.append(it)
}
}
}
if(strbuilder.get(0) == '.') strbuilder.deleteCharAt(0)
if(strbuilder.isNotEmpty() && strbuilder.get(strbuilder.length - 1) == '.') strbuilder.deleteCharAt(strbuilder.length -1)
if(strbuilder.isEmpty()) strbuilder.append('a')
if(strbuilder.length >=16){
val result = strbuilder.substring(0, 15)
if(result.get(result.length - 1) == '.') return result.substring(0, 14)
return result
}else if(strbuilder.length == 2){
strbuilder.append(strbuilder.get(strbuilder.length - 1))
}else if(strbuilder.length == 1){
strbuilder.append(strbuilder.get(strbuilder.length - 1))
strbuilder.append(strbuilder.get(strbuilder.length - 1))
}
return strbuilder.toString()
}
}
- μ κ·μμ μ¬μ©νλ©΄ νΈνκ² λ€λ μκ°μ νμ§λ§ λ°©λ²μ λͺ°λΌ λ³λλ‘ κ³΅λΆλ₯Ό νμ΅λλ€. μΌλ°μ μΌλ‘ μ½λ©ν μ€νΈμ ν΅κ³ΌνκΈ° μν μμ€μΌλ‘ μ κ·μμ λ°°μμΌ νλμ§μ λν μλ¬Έμ μμ§λ§ μλ©΄ ν¨μ¬ λΉ λ₯΄κ² ν μ μλ 건 μ¬μ€μ λλ€.
- μλ μ½λμ²λΌ λ©μλ 체μ΄λ κΈ°λ²μ μ¬μ©νλ©΄ κ°λ μ± λΏλ§ μλλΌ μ½λλ₯Ό μ λ§ μ§§κ² κ΅¬νν μ μμ΅λλ€. ν¨μν νλ‘κ·Έλλ°μ μλν¨μ λ νλ² λλΌλ€μ
class Solution {
fun solution(new_id: String): String = new_id.toLowerCase()
.filter { it.isLetter() || it.isDigit() || it in arrayOf('-','_','.') }
.replace("[.]+".toRegex(), ".")
.removePrefix(".")
.removeSuffix(".")
.let { if(it.isEmpty()) "a" else it }
.let { if(it.length >=16) it.substring(0, 15).removeSuffix(".") else it }
.let {
if(it.length == 1) it + it + it
else if(it.length == 2) it + it.last()
else it
}
}
μ°Έκ³
- ν¨μν νλ‘κ·Έλλ° μΈμ΄μΈ kotlinμ νΉμ±μ λ§κ² ν¨μλ₯Ό λ§μ΄ μ΅νμ π½
- https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/replace.html
λ°μν