Compare the lines in java and remove the part of the line where they are identical

I have two lines with me:

s1="MICROSOFT" s2="APPLESOFT" 

I need to compare the rows and remove the duplicated part (always towards the end) from the second row. Therefore, I should get "MICROSOFT" and "APPLE" as the output.

I compared the character of a string by character.

  String s1 = "MICROSOFT"; String s2 = "APPLESOFT"; for(int j=0; j<s1.length(); j++) { char c1 = s1.charAt(j); char c2 = s2.charAt(j); if(c1==c2) System.out.println("Match found!!!"); else System.out.println("No match found!"); } 

It should check the lines and if two lines have the same characters to the end of the line, then I need to remove this redundant part, SOFT in this case, from the second line. But I can’t figure out how to get out of here.

There may be more duplicates ... but we only need to remove those that are constantly identical. if I have APPWWSOFT and APPLESOFT, I have to get APPLE again in the second line, since we got a non-WW LE between

Can you guys help me here?

+7
source share
7 answers

I solved my problem after removing the brains. Please feel free to fix / improve / improve my code. The code not only works for the MICROSOFT and APPLESOFT inputs, but also for the APPWWSOFT and APPLESOFT inputs (I needed to remove continuous duplicates from the end - SOFT in both of the above inputs). I am at the training stage and I will be grateful for any valuable materials.

 public class test { public static void main(String[] args) { String s1 = "MICROSOFT"; String s2 = "APPLESOFT"; int counter1=0; int counter2=0; String[] test = new String[100]; test[0]=""; for(int j=0; j<s1.length(); j++) { char c1 = s1.charAt(j); char c2 = s2.charAt(j); if(c1==c2) { if(counter1==counter2) { //System.out.println("Match found!!!"); test[0]=test[0]+c2; counter2++; //System.out.println("Counter 2: "+counter2); } else test[0]=""; } else { //System.out.print("No match found!"); //System.out.println("Counter 2: "+counter2); counter2=counter1+1; test[0]=""; } counter1++; //System.out.println("Counter 1: "+counter1); } System.out.println(test[0]); System.out.println(s2.replaceAll(test[0]," ")); } } 
0
source

Find and read the longest general subsequence , you can find efficient algorithms to search for LCS of two input strings. After finding the LCS input strings, it is easy to manipulate the inputs. For example, in your case, the LCS algorithm will find "SOFT" as the LCS of these two lines, then you can check if the LCS is in the final part of the second input, and then easily delete it. Hope this idea helps.

Sample LCS code in Java here, try: http://introcs.cs.princeton.edu/java/96optimization/LCS.java.html

Example script (pseudo-code):

 input1: "MISROSOFT"; input2: "APPLESOFT"; execute LCS(input1, input2); store the result in lcs, now lcs = "SOFT"; iterate over the characters of input2, if a character exists in lcs then remove it from input2. 
+4
source

As far as I understand, you want to remove any identical characters from two lines. By identical, I mean: the same position and the same character (code). I think the following linear complexity solution is the simplest:

  StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); //if you want to remove the identical char //only from one string you don't need the 2nd sb char c; for(int i = 0; i<Math.min(s1.length,s2.length);i++){ if((c = s1.charAt(i)) != s2.charAt(i)){ sb1.append(c); } } return sb1.toString(); 
+2
source

Try this algorithm. Create character sequences for your first line and find it in the second line.

-
Middle case = (s1.length () - 1) sq

 public class SeqFind { public static String searchReplace(String s1,String s2) { String s3; boolean brk=false; for(int j=s1.length();j>0&&!brk;j--){ for (int i = j-4; i > 0; i--) { String string = s1.substring( i,j); if(s2.contains(string)){ System.out.println(s2+" - "+string+" "+s2.replace( string,"")); brk=true; break; } } } return s3; } public static void main(String[] args) { String s1 = "MICROSOFT"; String s2 = "APPLESOFT"; String s3 = searchReplace(s1,s2); } } 

Out put - APPLESOFT - SOFT - APPLE

+2
source
  public class Match { public static void main(String[] args) { String s1="MICROSOFT"; String s2="APPLESOFT"; String[] s=new String[10]; String s3; int j=0,k=0; for(int i=s2.length();i>0;i--) { s[j]=s2.substring(k,s2.length()); if(s1.contains(s[j])) { s3=s2.substring(0,j); System.out.println(s1+""+s3); System.exit(0); } else { System.out.println(""); } j++; k++; } } } 

I edited the code, which you can give it another try.

+1
source

try, you did not check

  String s1 = "MICROSOFT"; String s2 = "APPLESOFT"; String s3=""; for(int j=0; j<s1.length(); j++) { if(s1.charAt(j)==s2.charAt(j)){ s3+=s1.charAt(j); } } System.out.println(s1.replace(s3, " ") + " \n"+ s2.replace(s3, " ")); 
0
source

You should use a StringBuffer if you want your String be modified.

And in this case, you may have one additional StringBuffer in which you can continue to add an inappropriate character: -

  StringBuffer s1 = new StringBuffer("MICROSOFT"); StringBuffer s2 = new StringBuffer("APPLESOFT"); StringBuffer s3 = new StringBuffer(); for(int j=0; j<s1.length(); j++) { char c1 = s1.charAt(j); char c2 = s2.charAt(j); if(c1==c2) { System.out.println("Match found!!!"); } else { System.out.println("No match found!"); s3.append(c1); } } s1 = s3; System.out.println(s1); // Prints "MICRO" 
0
source

All Articles