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

Added anagram verifier #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* [Mergesort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/MergeSort.kt)
* [Quicksort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/QuickSort.kt)
* [Selectionsort](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/sort/SelectionSort.kt)
* String
* [Anagram](https://github.com/TheAlgorithms/Kotlin/blob/master/src/main/kotlin/string/Anagram.kt)
* Test
* Dynamic Programming
* [Palindromepartitioningtest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/dynamic_programming/PalindromePartitioningTest.kt)
Expand Down Expand Up @@ -72,3 +74,5 @@
* [Mergesorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/MergeSortTest.kt)
* [Quicksorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/QuickSortTest.kt)
* [Selectionsorttest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/sort/SelectionSortTest.kt)
* String
* [AnagramTest](https://github.com/TheAlgorithms/Kotlin/blob/master/src/test/kotlin/string/AnagramTest.kt)
48 changes: 48 additions & 0 deletions src/main/kotlin/string/Anagram.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package string


/**
* This method implements a simple anagram verifier algorithm
*
* @param string The string to be compared with
* @param ignoreWhitespace true if whitespaces should be ignored
* @param ignoreCase true if casing should be ignored
*
* Worst-case performance O(n)
* Best-case performance O(n)
* Average performance O(n)
* Worst-case space complexity O(N)
**/
fun String.isAnagramOf(string: String, ignoreWhitespace: Boolean = true, ignoreCase: Boolean = true): Boolean {
val string1 = this.normalize(ignoreWhitespace, ignoreCase)
val string2 = string.normalize(ignoreWhitespace, ignoreCase)

if (string1.length != string2.length)
return false

val charMap1 = string1.toCharCountMap()
val charMap2 = string2.toCharCountMap()

return charMap1 == charMap2
}

private fun String.toCharCountMap(): Map<Char, Int> {
val charMap = mutableMapOf<Char, Int>()

this.forEach {
val count = charMap[it] ?: 0
charMap[it] = count + 1
}

return charMap
}

private fun Sequence<Char>.removeWhitespaces() = this.filter { it.isWhitespace().not() }

private fun Sequence<Char>.toLowerCase() = this.map { it.toLowerCase() }

private fun String.normalize(ignoreWhitespace: Boolean, ignoreCase: Boolean) = this
.asSequence()
.let { if (ignoreCase) it.toLowerCase() else it }
.let { if (ignoreWhitespace) it.removeWhitespaces() else it }
.joinToString("")
95 changes: 95 additions & 0 deletions src/test/kotlin/string/AnagramTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package string

import org.junit.Test


class AnagramTest {

@Test
fun `test isAnagramOf empty string returns true`() {
val string1 = ""
val string2 = ""

assert(string1.isAnagramOf(string2))
}

@Test
fun `test isAnagramOf single letter string returns true`() {
val string1 = "a"
val string2 = "a"

assert(string1.isAnagramOf(string2))
}

@Test
fun `test isAnagramOf returns true`() {
val string1 = "testIsAnagramOfReturnsTrue1"
val string2 = "test isAnagramOf returns true 1"

assert(string1.isAnagramOf(string2))
}

@Test
fun `test isAnagramOf ignoreWhitespace true ignoreCase True returns True`() {
val string1 = "Wolfgang Amadeus Mozart"
val string2 = "A famous german waltz god"

assert(string1.isAnagramOf(string2, ignoreWhitespace = true, ignoreCase = true))
}

@Test
fun `test isAnagramOf ignoreWhitespace False ignoreCase True returns True`() {
val string1 = "Eleven plus Two"
val string2 = "Twelve plus One"

assert(string1.isAnagramOf(string2, ignoreWhitespace = false, ignoreCase = true))
}

@Test
fun `test isAnagramOf ignoreWhitespace True ignoreCase False returns True`() {
val string1 = "wolfgang amadeus mozart"
val string2 = "a famous german waltz god"

assert(string1.isAnagramOf(string2, ignoreWhitespace = true, ignoreCase = false))
}

@Test
fun `test isAnagramOf ignoreWhitespace False ignoreCase False returns True`() {
val string1 = "wolfgangamadeusmozart"
val string2 = "afamousgermanwaltzgod"

assert(string1.isAnagramOf(string2, ignoreWhitespace = false, ignoreCase = false))
}

@Test
fun `test isAnagramOf ignoreWhitespace true ignoreCase True returns False`() {
val string1 = "testIsAnagramOfReturnsTrue1"
val string2 = "test isAnagramOf returns true 2"

assert(string1.isAnagramOf(string2, ignoreWhitespace = true, ignoreCase = true).not())
}

@Test
fun `test isAnagramOf ignoreWhitespace False ignoreCase True returns False`() {
val string1 = "testIsAnagramOfReturnsTrue1"
val string2 = "test IsAnagramOf Returns True 1"

assert(string1.isAnagramOf(string2, ignoreWhitespace = false, ignoreCase = true).not())
}

@Test
fun `test isAnagramOf ignoreWhitespace True ignoreCase False returns False`() {
val string1 = "testIsAnagramOfReturnsTrue1"
val string2 = "testisAnagramOfreturnstrue1"

assert(string1.isAnagramOf(string2, ignoreWhitespace = true, ignoreCase = false).not())
}

@Test
fun `test isAnagramOf ignoreWhitespace False ignoreCase False returns False`() {
val string1 = "testisanagramofreturnstrue"
val string2 = "testisanagramofreturnstrup"

assert(string1.isAnagramOf(string2, ignoreWhitespace = false, ignoreCase = false).not())
}
}