Skip to content

Commit

Permalink
DP # LCS
Browse files Browse the repository at this point in the history
  • Loading branch information
harrypotter0 committed Jan 21, 2018
1 parent ed5563e commit 602dd86
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
19 changes: 19 additions & 0 deletions CP/January Challenge 2018/Untitled Document
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def longest_common_subsequence(sequence1, sequence2):
cols = len(sequence1) + 1 # Add 1 to represent 0 valued column for DP
rows = len(sequence2) + 1 # Add 1 to represent 0 valued row for DP

T = [[0 for _ in range(cols)] for _ in range(rows)]

max_length = 0

for i in range(1, rows):
for j in range(1, cols):
if sequence2[i - 1] == sequence1[j - 1]:
T[i][j] = 1 + T[i - 1][j - 1]
else:
T[i][j] = max(T[i - 1][j], T[i][j - 1])

max_length = max(max_length, T[i][j])

return max_length

19 changes: 19 additions & 0 deletions CP/January Challenge 2018/string.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
def longest_common_subsequence(sequence1, sequence2):
cols = len(sequence1) + 1 # Add 1 to represent 0 valued column for DP
rows = len(sequence2) + 1 # Add 1 to represent 0 valued row for DP

T = [[0 for _ in range(cols)] for _ in range(rows)]

max_length = 0

for i in range(1, rows):
for j in range(1, cols):
if sequence2[i - 1] == sequence1[j - 1]:
T[i][j] = 1 + T[i - 1][j - 1]
else:
T[i][j] = max(T[i - 1][j], T[i][j - 1])

max_length = max(max_length, T[i][j])

return max_length

5 changes: 0 additions & 5 deletions dynamic/longest_common_subsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
Given two sequences A = [A1, A2, A3,..., An] and B = [B1, B2, B3,..., Bm], find the length of the longest common
subsequence.
Video
-----
* https://youtu.be/NnD96abizww
Complexity
----------
Expand Down

0 comments on commit 602dd86

Please sign in to comment.