Skip to content

Commit

Permalink
Create Scala solution of problem 115.
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-per-ardua committed Oct 31, 2023
1 parent 1b9858a commit e4a6e27
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Hard/115. Distinct Subsequences/Approach 1/solution.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object Solution {
def numDistinct(s: String, t: String): Int = {
val m = s.length
val n = t.length

if (m < n) {
return 0
}

val dp: Array[Array[Int]] = Array.fill(m + 1, n + 1)(0)
for (i <- 0 to m) {
dp(i)(0) = 1
}

for (i <- 1 to m) {
for (j <- 1 to n) {
if (s(i-1) == t(j-1)) {
dp(i)(j) = dp(i-1)(j-1) + dp(i-1)(j)
} else {
dp(i)(j) = dp(i-1)(j)
}
}
}

dp(m)(n)
}
}

0 comments on commit e4a6e27

Please sign in to comment.