forked from igorwojda/kotlin-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.kt
36 lines (30 loc) · 1.05 KB
/
solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.igorwojda.string.isanagram
private object Solution1 {
private fun isAnagram(str1: String, str2: String): Boolean {
val a1 = str1.toUpperCase().filter { it.isLetter() }.groupBy { it }
val a2 = str2.toUpperCase().filter { it.isLetter() }.groupBy { it }
return a1 == a2
}
}
private object Solution2 {
private fun isAnagram(str1: String, str2: String): Boolean {
return getCharFrequency(str1) == getCharFrequency(str2)
}
private fun getCharFrequency(str: String): Map<Char, List<Char>> {
return str.toLowerCase()
.filter { it.isLetterOrDigit() }
.groupBy { it }
}
}
private object Solution3 {
private fun isAnagram(str1: String, str2: String): Boolean {
return getCharFrequency(str1) == getCharFrequency(str2)
}
private fun getCharFrequency(str: String): Map<Char, Int> {
return str.toLowerCase()
.filter { it.isLetterOrDigit() }
.groupingBy { it }
.eachCount()
}
}
private object KtLintWillNotComplain