【LeeCode】求两个字符串的公共字符

时间:2022-12-03 07:11:02

【题目描述】

  打印两个字符串的公共字符

【代码】

package com.company;
import java.util.*;

class Solution2 {

public void minWindow(String s1, String s2) {
char[] c1 = s1.toCharArray();
char[] c2 = s2.toCharArray();

Set<Character> set = new HashSet<>();
Set<Character> set2 = new HashSet<>();

for (char c : c1) {
set.add(c);
}

for (char c : c2) {
set2.add(c);
}

// 取交集的函数
set.retainAll(set2);
if (set.size() == 0 ){
System.out.println("no common chars");
}else{
System.out.println(set);
}

}
}
public class Test {
public static void main(String[] args) {
String str = "ADOBECODEBANC";
String sss = "ABC";
new Solution2().minWindow(str, sss);
}
}