Skip to content

Commit 729a834

Browse files
authored
Merge pull request #1019 from CodeHarborHub/dev-3
Debug Some issues
2 parents 0145bf6 + 87b9f9c commit 729a834

28 files changed

+333
-641
lines changed

dsa-solutions/lc-solutions/0000-0099/0001-two-sum.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ tags:
1111
- JavaScript
1212
- TypeScript
1313
description: "This is a solution to the Two Sum problem on LeetCode."
14+
sidebar_position: 1
1415
---
1516

1617
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.

dsa-solutions/lc-solutions/0000-0099/0002-Add-Two-Numbers.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ tags:
99
- Add Two Numbers
1010
- LeetCode
1111
description: "This is a solution to the Add Two Numbers problem on LeetCode."
12+
sidebar_position: 2
1213
---
1314

1415
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.

dsa-solutions/lc-solutions/0000-0099/0003-Longest-Substring-Without-Repeating-Characters.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ tags:
77
- String
88
- Sliding Window
99
description: "This is a solution to the Longest Substring Without Repeating Characters problem on LeetCode."
10+
sidebar_position: 3
1011
---
1112

1213
In this page we will solve the problem [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) from LeetCode.

dsa-solutions/lc-solutions/0000-0099/0004-Median-of-two-Sorted-Array.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ tags:
77
- Binary Search
88
- Divide and Conquer0
99
- LeetCode
10-
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))$.
10+
description: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
11+
sidebar_position: 4
1112
---
1213

1314
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.
Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
11
---
22
id: Longest Palindromic Substring
33
title: Longest Palindromic Substring(LeetCode)
4-
sidebar_label: 0005-Longest Palindrome Substring
5-
tags:
6-
- String
7-
- Dynamic Programming
4+
sidebar_label: 0005-Longest Palindrome Substring
5+
tags:
6+
- String
7+
- Dynamic Programming
88
description: Given a string s, return the longest palindromic substring in s.
9+
sidebar_position: 5
910
---
1011

11-
## Problem Description
12-
13-
| Problem Statement | Solution Link | LeetCode Profile |
14-
| :-------------------------------------------------------------------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------- |
15-
| [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/) |
16-
1712
## Problem Statement
1813

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

2116
### Examples
2217

2318
**Example 1:**
19+
2420
```plaintext
2521
Input: s = "babad"
2622
Output: "bab"
2723
Explanation: "aba" is also a valid answer.
2824
```
2925

3026
**Example 2:**
27+
3128
```plaintext
3229
Input: s = "cbbd"
3330
Output: "bb"
3431
```
3532

3633
### Constraints
34+
3735
- `1 <= s.length <= 1000`
3836
- `s` consist of only digits and English letters.
3937

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

4442
#### Intuition
43+
4544
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.
4645

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

5454
#### Implementation
55+
5556
```python
5657
def longestPalindrome(s: str) -> str:
5758
def check(l, r):
5859
while l >= 0 and r < len(s) and s[l] == s[r]:
5960
l -= 1
6061
r += 1
6162
return s[l+1:r]
62-
63+
6364
longest = ""
6465
for i in range(len(s)):
6566
odd_palindrome = check(i, i)
6667
even_palindrome = check(i, i + 1)
6768
longest = max(longest, odd_palindrome, even_palindrome, key=len)
68-
69+
6970
return longest
7071
```
7172

7273
### Complexity Analysis
74+
7375
- **Time complexity**: $O(n^3)$ - We check each substring, and checking each substring takes $O(n)$ time.
7476
- **Space complexity**: $O(1)$ - We use a constant amount of extra space.
7577

7678
### Approach 2: Dynamic Programming
7779

7880
#### Intuition
81+
7982
If we know a substring is a palindrome, we can extend it if the characters on both ends match.
8083

8184
#### Algorithm
85+
8286
1. Initialize a 2D DP table with `False`.
8387
2. Mark all single-character substrings as palindromes.
8488
3. Check substrings of length 2 and mark them if characters match.
8589
4. Use the table to extend palindromes to longer substrings.
8690
5. Keep track of the longest palindrome found.
8791

8892
#### Implementation
93+
8994
```python
9095
def longestPalindrome(s: str) -> str:
9196
n = len(s)
9297
dp = [[False] * n for _ in range(n)]
9398
longest = ""
94-
99+
95100
for i in range(n):
96101
dp[i][i] = True
97102
longest = s[i]
98-
103+
99104
for i in range(n-1):
100105
if s[i] == s[i+1]:
101106
dp[i][i+1] = True
102107
longest = s[i:i+2]
103-
108+
104109
for length in range(3, n+1):
105110
for i in range(n-length+1):
106111
j = i + length - 1
107112
if s[i] == s[j] and dp[i+1][j-1]:
108113
dp[i][j] = True
109114
longest = s[i:j+1]
110-
115+
111116
return longest
112117
```
113118

114119
### Complexity Analysis
120+
115121
- **Time complexity**: $O(n^2)$ - We fill an `n * n` table.
116122
- **Space complexity**: $O(n^2)$ - We use an `n * n` table to store the DP results.
117123

118124
### Approach 3: Expand From Centers
119125

120126
#### Intuition
127+
121128
A palindrome mirrors around its center. We can expand around potential centers to find palindromes.
122129

123130
#### Algorithm
131+
124132
1. Consider each character and pair of characters as potential centers.
125133
2. Expand around each center to find the longest palindrome.
126134
3. Keep track of the longest palindrome found.
127135

128136
#### Implementation
137+
129138
```python
130139
def longestPalindrome(s: str) -> str:
131140
def expandAroundCenter(s, left, right):
132141
while left >= 0 and right < len(s) and s[left] == s[right]:
133142
left -= 1
134143
right += 1
135144
return s[left + 1:right]
136-
145+
137146
if not s:
138147
return ""
139-
148+
140149
longest = s[0]
141-
150+
142151
for i in range(len(s)):
143152
odd_palindrome = expandAroundCenter(s, i, i)
144153
even_palindrome = expandAroundCenter(s, i, i + 1)
145154
longest = max(longest, odd_palindrome, even_palindrome, key=len)
146-
155+
147156
return longest
148157
```
149158

150159
### Complexity Analysis
151-
- **Time complexity**: $O(n^2)$ - We expand around each center, which takes $O(n)$ time.
160+
161+
- **Time complexity**: $O(n^2)$ - We expand around each center, which takes $O(n)$ time.
152162
- **Space complexity**: $O(1)$ - We use a constant amount of extra space.
153163

154164
### Approach 4: Manacher's Algorithm
155165

156166
#### Intuition
167+
157168
Manacher's algorithm can find the longest palindromic substring in linear time.
158169

159170
#### Algorithm
171+
160172
1. Transform the string to handle even-length palindromes.
161173
2. Use a helper array to store the length of the palindrome at each position.
162174
3. Use the properties of palindromes and the helper array to efficiently find the longest palindrome.
163175

164176
#### Implementation
177+
165178
```python
166179
def longestPalindrome(s: str) -> str:
167180
if not s:
168181
return ""
169-
182+
170183
# Transform s into new string with inserted boundaries
171184
T = '#'.join(f"^{s}$")
172185
n = len(T)
173186
P = [0] * n
174187
C = R = 0
175-
188+
176189
for i in range(1, n - 1):
177190
P[i] = (R > i) and min(R - i, P[2 * C - i])
178191
while T[i + P[i] + 1] == T[i - P[i] - 1]:
179192
P[i] += 1
180193
if i + P[i] > R:
181194
C, R = i, i + P[i]
182-
195+
183196
max_len, center_index = max((n, i) for i, n in enumerate(P))
184197
return s[(center_index - max_len) // 2: (center_index + max_len) // 2]
185198
```
186199

187200
### Complexity Analysis
201+
188202
- **Time complexity**: $O(n)$ - Manacher's algorithm runs in linear time.
189203
- **Space complexity**: $O(n)$ - We use an array of length `n` to store the lengths of palindromes.
190204

191205
### Conclusion
206+
192207
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.
208+
193209
```
194210
211+
```

0 commit comments

Comments
 (0)