Skip to content

Commit e9acc4d

Browse files
authored
Update Find_Longest_Common_Substring.java
1 parent 7bb9b67 commit e9acc4d

File tree

1 file changed

+76
-8
lines changed

1 file changed

+76
-8
lines changed

DynamicProgramming/LongestCommonSubstring/Find_Longest_Common_Substring.java

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
Using the DP Approach-
44
We construct a matrix to keep track of maximal length suffix of all substrings. We can find the maximal value from this matrix and traverse from that cell diognally upwards till the cell value becomes 0.
5-
5+
The below approach has a space complexity = O(n*m)
66
*/
77

88
public void printLCStr(String A, String B, int n, int m){
@@ -32,15 +32,83 @@ else if(A.charAt(i-1) == B.charAt(j-1)){
3232
}
3333

3434
if(maxLen == 0){
35-
System.out.println("No Substring");
35+
System.out.println("No Common Substring");
36+
}
37+
else{
38+
String resStr = "";
39+
while(lcs[row][col]!=0){
40+
resStr = A.charAt(row-1)+resStr;
41+
row--;
42+
col--;
43+
}
44+
System.out.println(resStr);
3645
}
46+
}
47+
48+
49+
/*
50+
Space Optimized Approach
51+
1. The space used by the above solution can be reduced to O(2*n).
52+
2. Variables used -
53+
2.1 end - used to store ending point of the longest common substring in string X
54+
2.2 maxlen - used to store the length of the longest common substring.
55+
3. How is Space Complexity reduced?
56+
Suppose we are at DP state when the length of X is i and length of Y is j, the result of which is stored in len[i][j].
57+
Now if X[i-1] == Y[j-1], then len[i][j] = 1 + len[i-1][j-1], that is result of current row in matrix len[][] depends on values from previous row.
58+
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).
59+
4. Print Longest Common Substring
60+
4.1 To print, we use variable end.
61+
4.2 When len[i][j] is calculated, it is compared with maxlen. If maxlen is less than len[i][j], then end is updated to i-1 to show that longest common substring ends at index i-1 in X and maxlen is updated to len[i][j].
62+
The longest common substring then is from index end – maxlen + 1 to index end in X.
63+
5. How to switch between rows?
64+
5.1 A variable currRow is used to represent that either row 0 or row 1 of len[2][n] matrix is currently used to find the length.
65+
5.2 Initially, row 0 is used as the current row for the case when the length of string X is zero. At the end of each iteration, the current row is made the previous row and the previous row is made the new current row.
66+
67+
*/
68+
69+
public void printStr(String A, String B){
70+
int n = A.length();
71+
int m = B.length();
72+
73+
int lcs[][] = new int[2][m];
74+
75+
//to store length of the longest common substring
76+
int maxLen = 0;
77+
78+
//to store the last index of the longest common substring
79+
int end = 0;
80+
81+
//to keep track of current row
82+
int currRow = 0;
3783

38-
String resStr = "";
39-
while(lcs[row][col]!=0){
40-
resStr = A.charAt(row-1)+resStr;
41-
row--;
42-
col--;
84+
for(int i=0;i<=n;i++){
85+
for(int j=0;j<=m;j++){
86+
if(i==0 || j==0) lcs[currRow][j] = 0;
87+
else if(A.charAt(i-1) == B.charAt(j-1)){
88+
lcs[currRow][j] = lcs[1-currRow][j] + 1;
89+
if(maxLen < lcs[currRow][j]){
90+
maxLen = lcs[currRow][j];
91+
end = i-1;
92+
}
93+
}
94+
else lcs[currRow][j] = 0;
95+
}
96+
97+
/*
98+
When currRow = 0, (1 - currRow) evaluates to 1 ==> currRow becomes 1
99+
when currRow = 1, (1 - currRow) evaluates to 0 ==> currRow becomes 0
100+
*/
101+
currRow = 1 - currRow;
43102
}
44103

45-
System.out.println(resStr);
104+
if(maxLen==0){
105+
System.out.println("No Common Substring");
106+
}
107+
else{
108+
System.out.println(X.substring(end-maxLen+1, end+1));
109+
}
46110
}
111+
/*
112+
Time Complexity - O(n*m)
113+
Space Complexity - O(n)
114+
*/

0 commit comments

Comments
 (0)