首页 > 其他分享 >【LeeCode】求两个字符串的公共字符

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

时间:2022-12-02 23:32:17浏览次数:38  
标签:字符 set String char LeeCode set2 字符串 new public

【题目描述】

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

【代码】

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

标签:字符,set,String,char,LeeCode,set2,字符串,new,public
From: https://blog.51cto.com/u_13682316/5907708

相关文章