题解 | #字符串字符匹配#
字符串字符匹配
https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String s = sc.nextLine();
String t = sc.nextLine();
HashSet<Character> simplestS = getSimplestString(s);
HashSet<Character> simplestT = getSimplestString(t);
String res = "true";
for(char c: simplestS){
//只要有一个短字符串中的字符不在长字符串中出现,立马停止循环,false
if (!simplestT.contains(c)){
res = "false";
break;
}
}
System.out.println(res);
}
sc.close();
}
//养成好习惯,代码太复杂了就写个函数处理,保持主函数的整洁
//HashSet自动去重
public static HashSet<Character> getSimplestString(String s) {
HashSet<Character> chars = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
chars.add(s.charAt(i));
}
return chars;
}
}