-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_9.java
More file actions
44 lines (32 loc) · 1.41 KB
/
Problem_9.java
File metadata and controls
44 lines (32 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package strings;
import java.util.*;
// Problem Title => Find The Longest Recurring Subsequence in String
public class Problem_9 {
// This function mainly returns LCS(str, str) with a condition that same
// characters at same index are not considered.
static int[][] dp = new int[1000][1000];
// Longest Repeated Subsequence Problem
static int findLongestRepeatingSubSeq(char[] X, int m, int n) {
if (dp[m][n] != -1)
return dp[m][n];
// return if we have reached the end of either string
if (m == 0 || n == 0)
return dp[m][n] = 0;
// if characters at index m and n matches and index is different
if (X[m - 1] == X[n - 1] && m != n)
return dp[m][n] = findLongestRepeatingSubSeq(X, m - 1, n - 1) + 1;
// else if characters at index m and n don't match
return dp[m][n] = Math.max(findLongestRepeatingSubSeq(X, m, n - 1), findLongestRepeatingSubSeq(X, m - 1, n));
}
// Longest Repeated Subsequence Problem
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int m = sc.nextInt();
for (int[] row : dp)
Arrays.fill(row, -1);
System.out.println("The length of the largest subsequence that repeats itself is : "
+ findLongestRepeatingSubSeq(str.toCharArray(), m, m));
sc.close();
}
}