Skip to content

Commit

Permalink
Merge pull request neetcode-gh#422 from dipti95/TypeScriptSolutions
Browse files Browse the repository at this point in the history
Added: TypeScript solutions 226,1143,268,43,7
  • Loading branch information
Ahmad-A0 authored Jul 13, 2022
2 parents 3447fc6 + c0dca17 commit 8504ff8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions typescript/1143-Longest-Common-Subsequence.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function longestCommonSubsequence(text1: string, text2: string): number {
let temp: number[][] = []
let max: number = 0
for (let i = 0; i <= text1.length; i++) {
temp.push(new Array(text2.length + 1).fill(0))
}

for (let i = 1; i < temp.length; i++) {
for (let j = 1; j < temp[0].length; j++) {
if (text1[i - 1] === text2[j - 1]) {
temp[i][j] = temp[i - 1][j - 1] + 1
} else {
temp[i][j] = Math.max(temp[i - 1][j], temp[i][j - 1])
}
max = Math.max(max, temp[i][j])
}
}

return max
}
8 changes: 8 additions & 0 deletions typescript/268-Missing-Number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function missingNumber(nums: number[]): number {
let sum: number = 0
let total: number = (nums.length * (nums.length + 1)) / 2
for (let i = 0; i < nums.length; i++) {
sum += nums[i]
}
return total - sum
}
21 changes: 21 additions & 0 deletions typescript/43-Multiply-Strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function multiply(num1: string, num2: string): string {
if (num1 === "0" || num2 === "0") {
return "0"
}

let maxLength: number = num1.length + num2.length
let result: Array<number> = Array(maxLength).fill(0)
for (let i = num2.length - 1; i >= 0; i--) {
let idx = maxLength - (num2.length - i)

for (let j = num1.length - 1; j >= 0; j--) {
const product =
result[idx] + parseInt(num1.charAt(j)) * parseInt(num2.charAt(i))
result[idx] = Math.floor(product % 10)
result[idx - 1] = Math.floor(product / 10) + result[idx - 1]
idx--
}
}
if (result[0] === 0) return result.slice(1).join("")
else return result.join("")
}

0 comments on commit 8504ff8

Please sign in to comment.