Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il
Hostgator 1 Penny

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


Distribute your Resume to 500+ Job Boards & Recruiters


6,849 questions

9,751 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
...