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) · 1016 Bytes
/
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.decapitalizeconst
// Kotlin idiomatic solution
private object Solution1 {
private fun decapitalizeConst(str: String): String {
val subsStringsList = str
.split("_")
.map { it.lowercase() }
.map { it.replaceFirstChar { string -> string.uppercaseChar() } }
return subsStringsList
.joinToString("")
.replaceFirstChar {
it.lowercaseChar()
}
}
}
// Another Approach
private object Solution2 {
private fun decapitalizeConst(str: String): String? {
val words = str.split("_").filter { it.isNotEmpty() }
if (words.size == 1) return words.first().lowercase()
return words.mapIndexed { index, word ->
if (index == 0) {
word.lowercase()
} else {
word.first().uppercase() + word.drop(1).lowercase()
}
}.joinToString(separator = "")
}
}
private object KtLintWillNotComplain