Skip to content

Commit 983f80b

Browse files
author
Partho Biswas
committed
Algoexpert solution updated
1 parent 9c82437 commit 983f80b

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
574574

575575
| # | Title | Solution | Tutorial | Level | Remarks |
576576
| --- | --- | --- | --- | --- | --- |
577-
|01| **[1055. Shortest Way to Form String](https://leetcode.com/problems/shortest-way-to-form-string/)**| [Python](https://tinyurl.com/wu6rdaw/1055_Shortest_Way_to_Form_String.py)| [Art 1](https://tinyurl.com/t6xap4c), [Art 2](https://leetcode.com/problems/shortest-way-to-form-string/discuss/330938/Accept-is-not-enough-to-get-a-hire.-Interviewee-4-follow-up) | Medium | 📌 **[Check BS approach again](https://tinyurl.com/t6xap4c)** |
577+
|01| **[1055. Shortest Way to Form String](https://tinyurl.com/rpwq5bx)**| [Python](https://tinyurl.com/wu6rdaw/1055_Shortest_Way_to_Form_String.py)| [Art 1](https://tinyurl.com/t6xap4c), [Art 2](https://tinyurl.com/y2nu3o52), [Art 3](https://tinyurl.com/wsjho6w), [Art 4](https://tinyurl.com/qrfuedy), [Art 5](https://tinyurl.com/tnzxjjz) | Medium | 📌 **Very important and hard problem** |
578578
|02| [1057. Campus Bikes](https://leetcode.com/problems/campus-bikes/)| [Python](https://tinyurl.com/wu6rdaw/1057_Campus_Bikes.py)| [Video 01](https://www.youtube.com/watch?v=tG7GFge4-fQ), [Article 01](https://leetcode.com/problems/campus-bikes/discuss/371604/Python-Solution-Using-Dictionary-With-Explanation)| Medium | 📌 Solve [1066. Campus Bikes II](https://leetcode.com/problems/campus-bikes-ii/) using [DP](https://leetcode.com/problems/campus-bikes-ii/discuss/303471/Python-DP-vs-Backtracking-with-comments), [DFS](https://leetcode.com/problems/campus-bikes-ii/discuss/303383/Python-DFS-with-memorization) and [Priority Queue](https://leetcode.com/problems/campus-bikes-ii/discuss/303422/Python-Priority-Queue) |
579579
|03| **[1007. Minimum Domino Rotations For Equal Row](https://tinyurl.com/y6qgt5fk)** | [Python](https://tinyurl.com/wu6rdaw/Minimum_Domino_Rotations_For_Equal_Row.py)| [Vid 1](https://tinyurl.com/uobop48), [Vid 2](https://tinyurl.com/qkqlcmv),[Art 1](https://tinyurl.com/ufvb3kg),[Art 2](https://tinyurl.com/u85uegn),[Art 3](https://tinyurl.com/yxq7q9v6) | Medium | 📌 Hard to spot if it is a Greedy. Very Important |
580580
|04| [406. Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)| [Python](https://tinyurl.com/wu6rdaw/406_Queue_Reconstruction_by_Height.py)| [Article 1](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89345/Easy-concept-with-PythonC%2B%2BJava-Solution), [Article 2](https://leetcode.com/problems/queue-reconstruction-by-height/discuss/89359/Explanation-of-the-neat-Sort%2BInsert-solution) | Medium | 📌 Fundamentals |
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
2-
3-
# Solution #1
4-
# O(nm*min(n, m)) time | O(nm*min(n, m)) space
5-
def longestCommonSubsequence(str1, str2):
6-
lcs = [[[] for x in range(len(str1) + 1)] for y in range(len(str2) + 1)]
7-
for i in range(1, len(str2) + 1):
8-
for j in range(1, len(str1) + 1):
9-
if str2[i - 1] == str1[j - 1]:
10-
lcs[i][j] = lcs[i - 1][j - 1] + [str2[i - 1]]
11-
else:
12-
lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1], key = len)
13-
return lcs[-1][-1]
14-
15-
16-
17-
18-
# Solution #2
1+
# Solution: My solution using 2d dp
192
# O(nm) time | O(nm) space
3+
def longestCommonSubsequence(string1, string2):
4+
dp = [[[] for _ in range(len(string2) + 1)] for _ in range(len(string1) + 1)]
5+
for i in range(1, len(string1) + 1):
6+
x = string1[i - 1]
7+
for j in range(1, len(string2) + 1):
8+
rx = string2[j - 1]
9+
if x == rx:
10+
dp[i][j] = dp[i - 1][j - 1] + [x]
11+
else:
12+
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j], key=len)
13+
return dp[-1][-1]
2014

2115

algoexpert.io/python/Longest_Palindromic_Substring.py

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
# https://www.algoexpert.io/questions/Longest%20Palindromic%20Substring
2-
3-
4-
# Solution 1
1+
# Algoexpert Provided Solution 1
52
# O(n^3) time | O(1) space
63
def longest_palindromic_substring(string):
74
longest = ""
@@ -24,7 +21,7 @@ def is_palindrome(string):
2421
return True
2522

2623

27-
# Solution 2 - prefered solution
24+
# Algoexpert Provided Solution 2 - prefered solution
2825
# O(n^2) time | O(1) space
2926
def longest_palindromic_substring(string):
3027
current_longest = [0, 1]
@@ -45,6 +42,27 @@ def get_longest_pelindrome_from(string, left_index, right_index):
4542
return [left_index + 1, right_index - 1]
4643

4744

45+
# My solution - using 2d dp - preferd solution
46+
def longestPalindromicSubstring(string):
47+
L = len(string)
48+
if L <= 1:
49+
return string
50+
51+
dp = [[None for _ in range(L)] for _ in range(L)]
4852

53+
# Populating diagonal line
54+
for i in range(L):
55+
dp[i][i] = string[i]
4956

57+
maxPalStr = ""
58+
for startIdx in range(L - 1, -1, -1):
59+
for endIdx in range(startIdx + 1, L):
60+
if string[startIdx] == string[endIdx]:
61+
if endIdx - startIdx == 1 or dp[startIdx + 1][endIdx - 1]:
62+
dp[startIdx][endIdx] = string[startIdx:endIdx + 1]
63+
maxPalStr = max(maxPalStr, dp[startIdx][endIdx], key=len)
64+
return maxPalStr
5065

66+
strig = "abcdefghfedcbaa"
67+
out = longestPalindromicSubstring(strig)
68+
print("Out: ", out)

0 commit comments

Comments
 (0)