From 9bf6211ec59e378d23cdfe1778b9103ff7bfdd78 Mon Sep 17 00:00:00 2001 From: keshav kumar Date: Wed, 14 Oct 2020 15:24:18 +0530 Subject: [PATCH] Create LCS.java Simple implementation for Longest Common Substring #1718 --- strings/LCS.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 strings/LCS.java diff --git a/strings/LCS.java b/strings/LCS.java new file mode 100644 index 000000000000..8374d215bfe8 --- /dev/null +++ b/strings/LCS.java @@ -0,0 +1,24 @@ +public class LCS { + + static String X, Y; + static int lcs(int i, int j, int count) { + + if (i == 0 || j == 0) { + return count; + } + + if (X.charAt(i - 1) == Y.charAt(j - 1)) { + count = lcs(i - 1, j - 1, count + 1); + } + count = Math.max(count, Math.max(lcs(i, j - 1, 0), + lcs(i - 1, j, 0))); + return count; + } + public static void main(String[] args) { + + int n, m; + X = "abbaf"; + Y = "abcdef"; + System.out.println(lcs(X.length(), Y.length(), 0)); + } +}