Skip to content

Commit

Permalink
Merge pull request bddicken#226 from dayanruben/feature/levenshtein-k…
Browse files Browse the repository at this point in the history
…otlin

Added Levenshtein's Kotlin code
  • Loading branch information
bddicken authored Dec 10, 2024
2 parents 3a4d870 + ea0cbf3 commit 115591b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fibonacci/kotlin/code.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fun fibonacci(n: Int): Int {
if (n == 0) return 0
if (n == 1) return 1
return fibonacci(n-1) + fibonacci(n-2)
return fibonacci(n - 1) + fibonacci(n - 2)
}

fun main(args: Array<String>) {
Expand Down
47 changes: 47 additions & 0 deletions levenshtein/kotlin/code.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
fun min(a: Int, b: Int, c: Int): Int = minOf(a, b, c)

fun levenshteinDistance(str1: String, str2: String): Int {
val m = str1.length
val n = str2.length
val matrix = Array(m + 1) { IntArray(n + 1) }

for (i in 0..m) {
matrix[i][0] = i
}
for (j in 0..n) {
matrix[0][j] = j
}

for (i in 1..m) {
for (j in 1..n) {
val cost = if (str1[i - 1] == str2[j - 1]) 0 else 1
matrix[i][j] = min(
matrix[i - 1][j] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j - 1] + cost
)
}
}

return matrix[m][n]
}

fun main(args: Array<String>) {
var minDistance = -1
var times = 0

for (i in args.indices) {
for (j in args.indices) {
if (i != j) {
val distance = levenshteinDistance(args[i], args[j])
if (minDistance == -1 || minDistance > distance) {
minDistance = distance
}
times++
}
}
}

println("times: $times")
println("min_distance: $minDistance")
}

0 comments on commit 115591b

Please sign in to comment.