702. 连接两个字符串中的不同字符
给出两个字符串, 你需要修改第一个字符串,将所有与第二个字符串中相同的字符删除, 并且第二个字符串中不同的字符与第一个字符串的不同字符连接
样例
样例 1:
输入 : s1 = "aacdb", s2 = "gafd"
输出 : "cbgf"
样例 2:
输入 : "abcs", s2 = "cxzca"
输出 : "bsxz"
public class Solution {
/**
* @param s1: the 1st string
* @param s2: the 2nd string
* @return: uncommon characters of given strings
*/
public String concatenetedString(String s1, String s2) {
// write your code here
char[] chars = s1.toCharArray();
char[] chars2 = s2.toCharArray();
ArrayList<Character> set = new ArrayList<>();
Set<Character> set2 = new LinkedHashSet<>();
for (char aChar : chars) {
set.add(aChar);
}
for (char aChar : chars2) {
if (set.contains(aChar)) {
set2.add(aChar);
}
}
for (char aChar : chars2) {
set.add(aChar);
}
set.removeAll(set2);
StringBuilder stringBuffer = new StringBuilder();
for (char value : set) {
stringBuffer.append(value);
}
return stringBuffer.toString();
}
}