String Algorithms for the day before your Coding Interview (Day before coding Interview Book 4) by Kiao Ue & Chatterjee Aditya

String Algorithms for the day before your Coding Interview (Day before coding Interview Book 4) by Kiao Ue & Chatterjee Aditya

Author:Kiao, Ue & Chatterjee, Aditya [Kiao, Ue]
Language: eng
Format: azw3
Publisher: OpenGenus
Published: 2020-05-09T16:00:00+00:00


for i from 0 to M + 1

for j from 0 to N + 1

if (i == 0 or j == 0 )

dp[i][j] = 0

else if (S1[i - 1 ] == S2[j - 1 ])

dp[i][j] = dp[i - 1 ][j - 1 ] + 1

if (result < dp[i][j])

{

result = dp[i][j];

r = i;

c = j;

}

else

dp[i][j] = 0

result is our answer

The basic condition of calculating dp[][] is result of current row in matrix dp[][] depends on values from previous row. Hence the required length of longest common substring can be obtained by maintaining values of two consecutive rows only , thereby reducing space requirements to O(2 * N) .

This would result in the following pseudocode:

dp[][] = 2D array 2 x M

result = 0 , r = 0 , c = 0

current = 0



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.