Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions README.md

Large diffs are not rendered by default.

15 changes: 6 additions & 9 deletions src/main/ts/g0001_0100/s0001_two_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,16 @@ You can return the answer in any order.
## Solution

```typescript
interface ITemp {
[key: number]: number
}

function twoSum(nums: number[], target: number): number[] {
const temp: ITemp = {}
const indexMap: Map<number, number> = new Map()
for (let i = 0; i < nums.length; i++) {
const tag = target - nums[i]
if (temp[tag] >= 0) {
return [temp[tag], i]
const requiredNum: number = target - nums[i]
if (indexMap.has(requiredNum)) {
return [indexMap.get(requiredNum)!, i]
}
temp[nums[i]] = i
indexMap.set(nums[i], i)
}
return [-1, -1]
}

export { twoSum }
Expand Down
42 changes: 21 additions & 21 deletions src/main/ts/g0001_0100/s0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,29 +54,29 @@ import { ListNode } from '../../com_github_leetcode/listnode'
* }
*/
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
const result = new ListNode(null)
let next = result
let carryNum = 0
let currSum = 0
let num1 = 0
let num2 = 0

while (l1 !== null || l2 !== null) {
// @ts-ignore
num1 = l1 !== null ? l1.val : 0
// @ts-ignore
num2 = l2 !== null ? l2.val : 0
currSum = (num1 + num2 + carryNum) % 10
carryNum = Math.floor((num1 + num2 + carryNum) / 10)
next.next = new ListNode(currSum)
next = next.next
l1 = l1 !== null ? l1.next : null
l2 = l2 !== null ? l2.next : null
const dummyHead: ListNode = new ListNode(0)
let p: ListNode | null = l1
let q: ListNode | null = l2
let curr: ListNode | null = dummyHead
let carry: number = 0
while (p !== null || q !== null) {
const x: number = p !== null ? p.val : 0
const y: number = q !== null ? q.val : 0
const sum: number = carry + x + y
carry = Math.floor(sum / 10)
curr.next = new ListNode(sum % 10)
curr = curr.next
if (p !== null) {
p = p.next
}
if (q !== null) {
q = q.next
}
}
if (carryNum) {
next.next = new ListNode(carryNum)
if (carry > 0) {
curr.next = new ListNode(carry)
}
return result.next
return dummyHead.next
}

export { addTwoNumbers }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,27 @@ Notice that the answer must be a substring, "pwke" is a subsequence and not a su

```typescript
function lengthOfLongestSubstring(s: string): number {
const hash: { [key: string]: number } = {}
let maxLength = 0
let length = 0
let start = 0
const lastIndices: number[] = new Array(256).fill(-1)
let maxLen: number = 0
let curLen: number = 0
let start: number = 0
for (let i = 0; i < s.length; i++) {
const char = s[i]
if (hash[char] !== undefined && hash[char] >= start) {
start = hash[char] + 1
length = i - start
const cur: string = s.charAt(i)
const charCode: number = cur.charCodeAt(0)
if (lastIndices[charCode] < start) {
lastIndices[charCode] = i
curLen++
} else {
const lastIndex: number = lastIndices[charCode]
start = lastIndex + 1
curLen = i - start + 1
lastIndices[charCode] = i
}
if (curLen > maxLen) {
maxLen = curLen
}
length++
hash[char] = i
maxLength = Math.max(maxLength, length)
}
return maxLength
return maxLen
}

export { lengthOfLongestSubstring }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,33 @@ Given a string `s`, return _the longest_ _palindromic_ _substring_ in `s`.

```typescript
function longestPalindrome(s: string): string {
let max = 0
let res = ''
const newStr: string[] = new Array(s.length * 2 + 1)
newStr[0] = '#'
for (let i = 0; i < s.length; i++) {
for (let j = i + 1; j <= s.length; j++) {
if (s.slice(i, j).length > max && isPalindrome(s.slice(i, j))) {
max = s.slice(i, j).length
res = s.slice(i, j)
}
}
newStr[2 * i + 1] = s.charAt(i)
newStr[2 * i + 2] = '#'
}
return res
}

const isPalindrome = (s: string): boolean => {
let i = 0
let j = s.length - 1
while (i < j) {
if (s[i] !== s[j]) {
return false
const dp: number[] = new Array(newStr.length)
let friendCenter: number = 0
let friendRadius: number = 0
let lpsCenter: number = 0
let lpsRadius: number = 0
for (let i = 0; i < newStr.length; i++) {
dp[i] =
friendCenter + friendRadius > i ? Math.min(dp[2 * friendCenter - i], friendCenter + friendRadius - i) : 1
while (i + dp[i] < newStr.length && i - dp[i] >= 0 && newStr[i + dp[i]] === newStr[i - dp[i]]) {
dp[i]++
}
if (friendCenter + friendRadius < i + dp[i]) {
friendCenter = i
friendRadius = dp[i]
}
if (lpsRadius < dp[i]) {
lpsCenter = i
lpsRadius = dp[i]
}
i++
j--
}
return true
return s.substring((lpsCenter - lpsRadius + 1) / 2, (lpsCenter + lpsRadius - 1) / 2)
}

export { longestPalindrome }
Expand Down
54 changes: 54 additions & 0 deletions src/main/ts/g0401_0500/s0416_partition_equal_subset_sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 416\. Partition Equal Subset Sum

Medium

Given a **non-empty** array `nums` containing **only positive integers**, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

**Example 1:**

**Input:** nums = [1,5,11,5]

**Output:** true

**Explanation:** The array can be partitioned as [1, 5, 5] and [11].

**Example 2:**

**Input:** nums = [1,2,3,5]

**Output:** false

**Explanation:** The array cannot be partitioned into equal sum subsets.

**Constraints:**

* `1 <= nums.length <= 200`
* `1 <= nums[i] <= 100`

## Solution

```typescript
function canPartition(nums: number[]): boolean {
let sums: number = 0
for (const num of nums) {
sums += num
}
if (sums % 2 === 1) {
return false
}
sums /= 2
const dp: boolean[] = new Array(sums + 1).fill(false)
dp[0] = true
for (const num of nums) {
for (let sum = sums; sum >= num; sum--) {
dp[sum] = dp[sum] || dp[sum - num]
}
}
return dp[sums]
}

export { canPartition }
```
63 changes: 63 additions & 0 deletions src/main/ts/g0401_0500/s0437_path_sum_iii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 437\. Path Sum III

Medium

Given the `root` of a binary tree and an integer `targetSum`, return _the number of paths where the sum of the values along the path equals_ `targetSum`.

The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/04/09/pathsum3-1-tree.jpg)

**Input:** root = [10,5,-3,3,2,null,11,3,-2,null,1], targetSum = 8

**Output:** 3

**Explanation:** The paths that sum to 8 are shown.

**Example 2:**

**Input:** root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22

**Output:** 3

**Constraints:**

* The number of nodes in the tree is in the range `[0, 1000]`.
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
* `-1000 <= targetSum <= 1000`

## Solution

```typescript
function pathSum(root: TreeNode | null, targetSum: number): number {
let count = 0
let map = new Map<number, number>()

function dfs(node: TreeNode | null, currentSum: number): void {
if (!node) return

currentSum += node.val
if (currentSum === targetSum) count++

count += map.get(currentSum - targetSum) ?? 0

map.set(currentSum, map.get(currentSum) + 1 || 1)
dfs(node?.left, currentSum)
dfs(node?.right, currentSum)

//remove from hashmap
map.set(currentSum, map.get(currentSum) - 1)
if (map.get(currentSum) === 0) map.delete(currentSum)
}

dfs(root, 0)
return count
}

export { pathSum }
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
[![](https://img.shields.io/github/stars/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Stars&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript)
[![](https://img.shields.io/github/forks/LeetCode-in-TypeScript/LeetCode-in-TypeScript?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-TypeScript/LeetCode-in-TypeScript/fork)

## 438\. Find All Anagrams in a String

Medium

Given two strings `s` and `p`, return _an array of all the start indices of_ `p`_'s anagrams in_ `s`. You may return the answer in **any order**.

An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

**Example 1:**

**Input:** s = "cbaebabacd", p = "abc"

**Output:** [0,6]

**Explanation:**

The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".

**Example 2:**

**Input:** s = "abab", p = "ab"

**Output:** [0,1,2]

**Explanation:**

The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".

**Constraints:**

* <code>1 <= s.length, p.length <= 3 * 10<sup>4</sup></code>
* `s` and `p` consist of lowercase English letters.

## Solution

```typescript
function findAnagrams(s: string, p: string): number[] {
const map: number[] = new Array(26).fill(0)
for (let i = 0; i < p.length; ++i) {
map[p.charCodeAt(i) - 'a'.charCodeAt(0)]++
}
const res: number[] = []
let i: number = 0
let j: number = 0

while (i < s.length) {
const idx: number = s.charCodeAt(i) - 'a'.charCodeAt(0)
// Add the new character
map[idx]--
// If the length is greater than window's length, pop the left character in the window
if (i >= p.length) {
map[s.charCodeAt(j++) - 'a'.charCodeAt(0)]++
}
let finish: boolean = true

for (let k = 0; k < 26; k++) {
// If it is not an anagram of string p
if (map[k] !== 0) {
finish = false
break
}
}

if (i >= p.length - 1 && finish) {
res.push(j)
}
i++
}
return res
}

export { findAnagrams }
```
Loading