Just take a look at how easy it is to solve this Easy-level Leetcode question using Kotlin:
class Solution {
fun defangIPaddr(address: String): String {
return address.map {
if(it.equals('.')) "[.]" else it
}.joinToString(separator = "")
}
}
It uses the map operator to iterate through each character. It then checks if the character is equal to . then replace it with [.], otherwise, just return the same character. Map operator returns a list of characters so in the end, we need to combine all of them into a single string by using joinToString.
Runtime: 156 ms, faster than 67.48% of Kotlin online submissions for Defanging an IP Address.
Memory Usage: 33.6 MB, less than 100.00% of Kotlin online submissions for Defanging an IP Address.