Created
April 2, 2021 14:00
-
-
Save kimji1/f5d61f3fc9b9f8c95aa61972b7d0b25f to your computer and use it in GitHub Desktop.
leetcode_409_Longest_Palindrome.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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