Files
grokking_algorithms/11_dynamic_programming/kotlin/LongestCommonSubsequence.kt
Mikhail 7d16656674 Add a solution to the problem on Kotlin (#269)
* Solution. Longest Common Subsequence
2024-03-22 14:51:00 -05:00

38 lines
1018 B
Kotlin

import kotlin.math.max
fun main() {
val wordA = "hish"
val wordB = "fish"
getLongestCommonSubSequence(wordA, wordB)
}
private fun getLongestCommonSubSequence(wordA: String, wordB: String) {
val cell = Array(wordA.length) { IntArray(wordB.length) }
for (i in wordA.indices) {
for (j in wordB.indices) {
// Буквы совпадают
if (wordA[i] == wordB[j]) {
if (i > 0 && j > 0) {
cell[i][j] = cell[i - 1][j - 1] + 1
} else {
cell[i][j] = 1
}
} else {
// Буквы не совпадают
if (i > 0 && j > 0) {
cell[i][j] = max(cell[i - 1][j], cell[i][j - 1])
} else {
cell[i][j] = 0
}
}
}
}
printResult(cell)
}
fun printResult(array: Array<IntArray>) {
for (row in array) {
println(row.contentToString())
}
}