Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Trees

Liquid Web Managed Dedicated Server: RAID 1 SSD with 16GB of RAM minimum! 24/7 On-Site Heroic Support! Industry-Leading SLA!

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Liquid Web Cloud VPS Hosting

8,954 questions

12,356 answers

573 users

How to find the length of longest common subsequence (LCS) present in two strings with Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
  
    int lcs(char[] s1, char[] s2, int s1_len, int s2_len)
    {
        if (s1_len == 0 || s2_len == 0)
            return 0;
        if (s1[s1_len - 1] == s2[s2_len - 1])
            return 1 + lcs(s1, s2, s1_len - 1, s2_len - 1);
        else
            return mymax(lcs(s1, s2, s1_len, s2_len - 1), lcs(s1, s2, s1_len - 1, s2_len));
    }

    int mymax(int a, int b)
    {
       return (a > b)? a : b;
    }
    public static void main(String[] args) {

        JavaApplication1 obj = new JavaApplication1();
        String s1 = "accyrb";
        String s2 = "cyxyazb";
 
        char[] cs1 = s1.toCharArray();
        char[] cs2 = s2.toCharArray();
 
        System.out.println("The length of LCS is: " + obj.lcs(cs1, cs2, cs1.length, cs2.length));
    }
}
    
/*
run:
   
The length of LCS is: 3
    
*/

 




answered Jun 28, 2017 by avibootz
...