-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(strings): is palindrome permutation
- Loading branch information
1 parent
cfae34b
commit f4c8333
Showing
3 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
30 changes: 30 additions & 0 deletions
30
ktstrings/src/main/kotlin/com/kotlinground/strings/palindromes/isPalindromePermutation.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.kotlinground.strings.palindromes | ||
|
||
/** | ||
* Checks if a given string is a palindrome permutation | ||
* @param someString [String] string to check for palindrome property | ||
* @return [Boolean] True if a palindrome, false otherwise | ||
*/ | ||
fun isPalindromePermutation(someString: String): Boolean { | ||
val normalizedString = someString.replace(" ", "").lowercase() | ||
val charCount = mutableMapOf<Char, Int>() | ||
|
||
for (char in normalizedString) { | ||
if (charCount.containsKey(char)) { | ||
charCount[char] = charCount[char]!! + 1 | ||
} else { | ||
charCount[char] = 1 | ||
} | ||
} | ||
|
||
var oddCount = 0 | ||
for (count in charCount.values) { | ||
val isEven = count % 2 != 0 | ||
if (isEven && oddCount == 0) { | ||
oddCount++ | ||
} else if (isEven && oddCount != 0) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
27 changes: 27 additions & 0 deletions
27
...rings/src/test/kotlin/com/kotlinground/strings/palindromes/IsPalindromePermutationTest.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.kotlinground.strings.palindromes | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class IsPalindromePermutationTest { | ||
@Test | ||
fun `should return true for 'Tact Coa'`() { | ||
val s = "Tact Coa" | ||
val actual = isPalindromePermutation(s) | ||
assertTrue(actual) | ||
} | ||
|
||
@Test | ||
fun `should return true for 'This is not a palindrome permutation'`() { | ||
val s = "This is not a palindrome permutation" | ||
val actual = isPalindromePermutation(s) | ||
assertTrue(actual) | ||
} | ||
|
||
@Test | ||
fun `should return true for 'taco cat'`() { | ||
val s = "taco cat" | ||
val actual = isPalindromePermutation(s) | ||
assertTrue(actual) | ||
} | ||
} |