[BOJ 실버1] 회문

문제

코드

fun main() = with(System.`in`.bufferedReader()) {
    val n = readLine().toInt()
    repeat(n){
        println(Check(readLine()))
    }
}

fun Check(str : String) : Int{
    for(i in 0..str.length/2){
        if(str[i]!=str[str.length-1-i]){
            var sb = StringBuilder()
            sb.append(str)
            sb.deleteCharAt(i)
            when(isPalin(sb.toString())){
                true -> return 1
            }
            sb.clear()
            sb.append(str)
            sb.deleteCharAt(str.length-1-i)
            when(isPalin(sb.toString())){
                true -> return 1
                else -> return 2
            }
        }
    }
    return 0
}

fun isPalin(str : String) : Boolean{
    for(i in 0..str.length/2){
        if(str[i]!=str[str.length-1-i]){
            return false
        }
    }
    return true
}

좋은 웹페이지 즐겨찾기