Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permutation palindrome solution is wrong. #169

Open
RoryKelly opened this issue Feb 10, 2024 · 0 comments
Open

Permutation palindrome solution is wrong. #169

RoryKelly opened this issue Feb 10, 2024 · 0 comments

Comments

@RoryKelly
Copy link

RoryKelly commented Feb 10, 2024

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 RoryKelly changed the title Permutation palindrom solution is wrong. Permutation palindrome solution is wrong. Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant