Note that there are some explanatory texts on larger screens.

plurals
  1. POFind Longest common subsequence : my method
    text
    copied!<p>I am trying to implement the Longest common sub sequence of 2 strings. My method is bit different than posted here. I am near the solution but little problem. Please help me.</p> <p>My problem: Desired Output: metallica current output: etallica</p> <p>My code is :</p> <pre><code>import java.util.ArrayList; import java.util.Stack; public class CommonSubS { public static void main(String[] args) { ArrayList&lt;Character&gt; ss = new ArrayList&lt;&gt;(); String s1 = "metallicarules"; String s2 = "rulmetallicales"; ArrayList&lt;ArrayList&lt;Character&gt;&gt; one = new ArrayList&lt;&gt;(); char[] first = s1.toCharArray(); char[] second = s2.toCharArray(); int j = 0; int current = 0; int mainIndex = 0; ArrayList&lt;Character&gt; sec = new ArrayList&lt;&gt;(); // search for each char in second for(int i = 0; i&lt;second.length; i++) { j = current; // search for each char in first while(j&lt;first.length) { // if match found, add to the arraylist if(first[j] == second[i]){ sec.add(first[j]); current = j+1; break; } else { // if different char occured in between, // save current arraylist in one // and go forward one.add(sec); sec = new ArrayList&lt;&gt;(); j++; current = 0; } } } for(ArrayList&lt;Character&gt; s: one) { for(Character c: s) { System.out.print(c); } System.out.println(); } } } /* desired output: metallica current output: etallica */ </code></pre> <p>I know there are other methods but I tried to implement using simple method. Please point out the problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload