-2

Input:

String str1="AUTOMATION " ; String str2="SELENIUM".

Output :

String str1="ATOATO" ; String str2="SELE";
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281

2 Answers2

1

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");
    }
}
0

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

Vignesh Sekar
  • 36
  • 1
  • 5