Input:
String str1="AUTOMATION " ; String str2="SELENIUM".
Output :
String str1="ATOATO" ; String str2="SELE";
Input:
String str1="AUTOMATION " ; String str2="SELENIUM".
Output :
String str1="ATOATO" ; String str2="SELE";
Use this :
public class CommonCharacter {
public void removeCommonCharacter(String s1, String s2){
String commonChars = "";
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
if (s1.charAt(i) == s2.charAt(j)) {
commonChars += s1.charAt(i);
}
}
}
for(int i = 0; i < commonChars.length(); i ++) {
String charToRemove = commonChars.charAt(i)+"";
s1 = s1.replace(charToRemove, "");
s2 = s2.replace(charToRemove, "");
}
System.out.println("After removing common character " + s1);
System.out.println("After removing common character " + s2);
}
public static void main(String[] args){
CommonCharacter commonCharacter = new CommonCharacter();
commonCharacter.removeCommonCharacter("AUTOMATION", "SELENIUM");
}
}
To remove the common characters in the two strings, use the below method:
public void removeDuplicateChar(String s1, String s2) {
String newS1 = s1, newS2 = s2;
ArrayList<Integer> s1Arr = new ArrayList<Integer>();
ArrayList<Integer> s2Arr = new ArrayList<Integer>();
for (int i = 0; i < s1.length(); i++) {
for (int j = 0; j < s2.length(); j++) {
if (s1.charAt(i) == s2.charAt(j)) {
s1Arr.add(i);
s2Arr.add(j);
}
}
}
for (int k : s1Arr) {
newS1 = newS1.replace((String) (s1.charAt(k) + ""), "");
}
for (int l : s2Arr) {
newS2 = newS2.replace((String) (s2.charAt(l) + ""), "");
}
System.out.println("The modified Strings are: "+ newS1 +" "+ newS2);
}
Not the simplest or fastest way, but it gets the job done