shriyanshi24jindal

Longest Common Substring

Mar 28th, 2026
13
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
Java 21.78 KB | None | 0 0
  1. // import java.util.Math;
  2.  
  3. class Solution {
  4. /**
  5.   * Key Idea
  6.   * Separate: current substring length (returned)
  7.   * maximum substring length (stored globally)
  8.   * Because substring must remain continuous.
  9.   */
  10.  
  11. int maxLength = 0;
  12.  
  13. /** Recursion */
  14. public int longCommSubstr(String s1, String s2) {
  15. int n = s1.length();
  16. int m = s2.length();
  17.  
  18. findLength(s1, s2, n, m);
  19. return maxLength;
  20. }
  21.  
  22. public int findLength(String s1, String s2, int n, int m)
  23. {
  24. if(n == 0 || m == 0)
  25. return 0;
  26.  
  27. int currentLength = 0;
  28. // when character matches, simply increment the length
  29. if(s1.charAt(n-1) == s2.charAt(m-1))
  30. {
  31. currentLength = 1 + findLength(s1, s2, n-1, m-1);
  32. maxLength = Math.max(currentLength, maxLength);
  33. }
  34. // when characters are different, so we cannot continue with current substring,
  35. findLength(s1, s2, n-1, m);
  36. findLength(s1, s2, n, m-1);
  37. return currentLength;
  38. }
  39.  
  40. /** Memoization */
  41. public int longCommSubstr(String s1, String s2) {
  42. int n = s1.length();
  43. int m = s2.length();
  44.  
  45. // step-1 create a matrix
  46. int[][] t = new int[n+1][m+1];
  47.  
  48. // step-2 initialise the matrix
  49. for(int i=0; i<n+1; i++)
  50. Arrays.fill(t[i], -1);
  51.  
  52. findLength(s1, s2, n, m, t);
  53. return maxLength;
  54. }
  55.  
  56. public int findLength(String s1, String s2, int n, int m, int[][] t)
  57. {
  58. if(n == 0 || m == 0)
  59. return 0;
  60.  
  61. if(t[n][m] != -1)
  62. return t[n][m];
  63.  
  64.  
  65. int currentLength = 0;
  66. // when character matches, simply increment the length
  67. if(s1.charAt(n-1) == s2.charAt(m-1))
  68. {
  69. // increase the current length
  70. currentLength = 1 + findLength(s1, s2, n-1, m-1, t);
  71. maxLength = Math.max(currentLength, maxLength);
  72. }
  73.  
  74. // when characters are different, so we cannot continue with current substring,
  75. findLength(s1, s2, n-1, m, t);
  76. findLength(s1, s2, n, m-1, t);
  77. t[n][m] = currentLength;
  78. return t[n][m];
  79. }
  80.  
  81. /** Top-Down approach */
  82. public int longCommSubstr(String s1, String s2) {
  83. int n = s1.length();
  84. int m = s2.length();
  85.  
  86. // step-1 create a matrix
  87. int[][] t = new int[n+1][m+1];
  88.  
  89. // step-2 initialise the matrix with base condition
  90. for(int i=0; i<n+1; i++)
  91. {
  92. for(int j=0; j<m+1; j++)
  93. {
  94. if(i == 0 || j == 0)
  95. t[i][j] = 0;
  96. }
  97. }
  98.  
  99. // step-3 convert recursive code into iterative
  100. for(int i=1; i<n+1; i++)
  101. {
  102. for(int j=1; j<m+1; j++)
  103. {
  104. int currentLength = 0;
  105. if(s1.charAt(i-1) == s2.charAt(j-1))
  106. {
  107. currentLength = 1 + t[i-1][j-1];
  108. maxLength = Math.max(currentLength, maxLength);
  109. t[i][j] = currentLength;
  110. }
  111. else
  112. t[i][j] = 0; // discontiue the substring and mark length as 0
  113. }
  114. }
  115. return maxLength;
  116. }
  117. }
RAW Paste Data Copied