CodeWars: Longest Common Subsequence (Performance Version) Solution
1var recursion = function (text1, text2, i, j, memo) {2 if (memo[i][j] !== null) {3 return memo[i][j]4 }56 if (i === 0 || j === 0) {7 memo[i][j] = ""8 } else if (text1[i - 1] === text2[j - 1]) {9 memo[i][j] = text1[i - 1] + recursion(text1, text2, i - 1, j - 1, memo)10 } else {11 let [one, two] = [12 recursion(text1, text2, i, j - 1, memo),13 recursion(text1, text2, i - 1, j, memo),14 ]15 if (one.length > two.length) {16 memo[i][j] = one17 } else {18 memo[i][j] = two19 }20 }2122 return memo[i][j]23}2425function lcs(x, y) {26 let memo = Array.from({ length: x.length + 1 }, (_, i) =>27 Array(y.length + 1).fill(null)28 )29 const res = recursion(x, y, x.length, y.length, memo)30 return res.split("").reverse().join("")31}
Comments
Loading comments...
Tags
codewars
dynamic programming
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
CodeWars: Objectify A Url Query String
Feb 9, 2021
Previous Post
CodeWars: Remove Zeroes
Feb 9, 2021