kotlin-coding-challenges icon indicating copy to clipboard operation
kotlin-coding-challenges copied to clipboard

Permutation palindrome solution is wrong.

Open RoryKelly opened this issue 1 year ago • 0 comments

This test will fail even though oooo is a valid palindrome and palindrome Permutation

    @Test
    fun `'oooo' a palindrome`() {
        isPermutationPalindrome("oooo") shouldBeEqualTo true
    }

solution should be

private fun isPermutationPalindrome(str: String): Boolean {
    val charCountMap = str.groupingBy { it }.eachCount()
    val oddCount = charCountMap.count { it.value % 2 != 0 }

    return if (str.length % 2 == 0) {
        oddCount == 0
    } else {
        oddCount == 1
    }
}

RoryKelly avatar Feb 10 '24 20:02 RoryKelly