Skip to content

Debug Some issues #1019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 11, 2024
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
1 change: 1 addition & 0 deletions dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tags:
- JavaScript
- TypeScript
description: "This is a solution to the Two Sum problem on LeetCode."
sidebar_position: 1
---

In this page, we will solve the Two Sum problem using three different approaches: brute force, hash table, and two-pointer technique. We will provide the implementation of the solution in JavaScript, TypeScript, Python, Java, C++, and more.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ tags:
- Add Two Numbers
- LeetCode
description: "This is a solution to the Add Two Numbers problem on LeetCode."
sidebar_position: 2
---

In this page, we will solve the Add Two Numbers problem. We will provide an overview of the problem, the intuition and approach to solve it, the implementation, and the code in various languages, along with a detailed explanation of the solution and its complexity analysis. Let's start by understanding the problem.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tags:
- String
- Sliding Window
description: "This is a solution to the Longest Substring Without Repeating Characters problem on LeetCode."
sidebar_position: 3
---

In this page we will solve the problem [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) from LeetCode.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ tags:
- Binary Search
- Divide and Conquer0
- LeetCode
description: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be $O(log (m+n))$.
description: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
sidebar_position: 4
---

In this page, we will solve the problem [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/description/) from LeetCode. We will provide multiple approaches to solve the problem along with the code implementation and the complexity analysis of the code.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
---
id: Longest Palindromic Substring
title: Longest Palindromic Substring(LeetCode)
sidebar_label: 0005-Longest Palindrome Substring
tags:
- String
- Dynamic Programming
sidebar_label: 0005-Longest Palindrome Substring
tags:
- String
- Dynamic Programming
description: Given a string s, return the longest palindromic substring in s.
sidebar_position: 5
---

## Problem Description

| Problem Statement | Solution Link | LeetCode Profile |
| :-------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------- |
| [Longest Palindromic Substring on LeetCode](https://leetcode.com/problems/longest-palindromic-substring/description/) | [Longest Palindromic Substring Solution on LeetCode](https://leetcode.com/problems/longest-palindromic-substring/editorial/) | [gabaniyash846](https://leetcode.com/u/gabaniyash846/) |

## Problem Statement

Given a string `s`, return the longest palindromic substring in `s`.

### Examples

**Example 1:**

```plaintext
Input: s = "babad"
Output: "bab"
Explanation: "aba" is also a valid answer.
```

**Example 2:**

```plaintext
Input: s = "cbbd"
Output: "bb"
```

### Constraints

- `1 <= s.length <= 1000`
- `s` consist of only digits and English letters.

Expand All @@ -42,153 +40,172 @@ Output: "bb"
### Approach 1: Check All Substrings

#### Intuition

We can start with a brute-force approach. We will simply check if each substring is a palindrome, and take the longest one that is.

#### Algorithm

1. Create a helper method `check(i, j)` to determine if a substring is a palindrome.
2. Use two pointers to iterate from the start and end of the substring towards the center.
3. If characters at both pointers are equal, move the pointers inward.
4. Use nested loops to check all possible substrings and use the helper method to determine if they are palindromes.
5. Keep track of the longest palindrome found.

#### Implementation

```python
def longestPalindrome(s: str) -> str:
def check(l, r):
while l >= 0 and r < len(s) and s[l] == s[r]:
l -= 1
r += 1
return s[l+1:r]

longest = ""
for i in range(len(s)):
odd_palindrome = check(i, i)
even_palindrome = check(i, i + 1)
longest = max(longest, odd_palindrome, even_palindrome, key=len)

return longest
```

### Complexity Analysis

- **Time complexity**: $O(n^3)$ - We check each substring, and checking each substring takes $O(n)$ time.
- **Space complexity**: $O(1)$ - We use a constant amount of extra space.

### Approach 2: Dynamic Programming

#### Intuition

If we know a substring is a palindrome, we can extend it if the characters on both ends match.

#### Algorithm

1. Initialize a 2D DP table with `False`.
2. Mark all single-character substrings as palindromes.
3. Check substrings of length 2 and mark them if characters match.
4. Use the table to extend palindromes to longer substrings.
5. Keep track of the longest palindrome found.

#### Implementation

```python
def longestPalindrome(s: str) -> str:
n = len(s)
dp = [[False] * n for _ in range(n)]
longest = ""

for i in range(n):
dp[i][i] = True
longest = s[i]

for i in range(n-1):
if s[i] == s[i+1]:
dp[i][i+1] = True
longest = s[i:i+2]

for length in range(3, n+1):
for i in range(n-length+1):
j = i + length - 1
if s[i] == s[j] and dp[i+1][j-1]:
dp[i][j] = True
longest = s[i:j+1]

return longest
```

### Complexity Analysis

- **Time complexity**: $O(n^2)$ - We fill an `n * n` table.
- **Space complexity**: $O(n^2)$ - We use an `n * n` table to store the DP results.

### Approach 3: Expand From Centers

#### Intuition

A palindrome mirrors around its center. We can expand around potential centers to find palindromes.

#### Algorithm

1. Consider each character and pair of characters as potential centers.
2. Expand around each center to find the longest palindrome.
3. Keep track of the longest palindrome found.

#### Implementation

```python
def longestPalindrome(s: str) -> str:
def expandAroundCenter(s, left, right):
while left >= 0 and right < len(s) and s[left] == s[right]:
left -= 1
right += 1
return s[left + 1:right]

if not s:
return ""

longest = s[0]

for i in range(len(s)):
odd_palindrome = expandAroundCenter(s, i, i)
even_palindrome = expandAroundCenter(s, i, i + 1)
longest = max(longest, odd_palindrome, even_palindrome, key=len)

return longest
```

### Complexity Analysis
- **Time complexity**: $O(n^2)$ - We expand around each center, which takes $O(n)$ time.

- **Time complexity**: $O(n^2)$ - We expand around each center, which takes $O(n)$ time.
- **Space complexity**: $O(1)$ - We use a constant amount of extra space.

### Approach 4: Manacher's Algorithm

#### Intuition

Manacher's algorithm can find the longest palindromic substring in linear time.

#### Algorithm

1. Transform the string to handle even-length palindromes.
2. Use a helper array to store the length of the palindrome at each position.
3. Use the properties of palindromes and the helper array to efficiently find the longest palindrome.

#### Implementation

```python
def longestPalindrome(s: str) -> str:
if not s:
return ""

# Transform s into new string with inserted boundaries
T = '#'.join(f"^{s}$")
n = len(T)
P = [0] * n
C = R = 0

for i in range(1, n - 1):
P[i] = (R > i) and min(R - i, P[2 * C - i])
while T[i + P[i] + 1] == T[i - P[i] - 1]:
P[i] += 1
if i + P[i] > R:
C, R = i, i + P[i]

max_len, center_index = max((n, i) for i, n in enumerate(P))
return s[(center_index - max_len) // 2: (center_index + max_len) // 2]
```

### Complexity Analysis

- **Time complexity**: $O(n)$ - Manacher's algorithm runs in linear time.
- **Space complexity**: $O(n)$ - We use an array of length `n` to store the lengths of palindromes.

### Conclusion

We discussed four approaches to solve the "Longest Palindromic Substring" problem, each with varying complexities and trade-offs. The dynamic programming and center expansion approaches provide a good balance of simplicity and efficiency for practical use cases, while Manacher's algorithm offers the optimal theoretical performance.

```

```
Loading
Loading