Skip to content

Instantly share code, notes, and snippets.

@kimji1
Created April 2, 2021 14:00
Show Gist options
  • Save kimji1/f5d61f3fc9b9f8c95aa61972b7d0b25f to your computer and use it in GitHub Desktop.
Save kimji1/f5d61f3fc9b9f8c95aa61972b7d0b25f to your computer and use it in GitHub Desktop.
leetcode_409_Longest_Palindrome.kt
class Solution {
fun longestPalindrome(s: String): Int {
val map = mutableMapOf<Int, Int>()
s.forEach {
val value = map[it.toInt()]
map[it.toInt()] = if (value == null) 1 else value + 1
}
val sumEvens = map.filter { it.value % 2 == 0 }
.values
.sum()
val odds = map.filter { it.value % 2 != 0 }.values
val sumOdds = if (odds.sum() == 0) 0 else odds.sum() - odds.size + 1
return sumEvens + sumOdds
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment