题解 | #字符串字符匹配#
字符串字符匹配
https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
import java.util.HashSet; import java.util.Scanner; import java.util.Set; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { /** * 判断短字符串S中的所有字符是否在长字符串T中全部出现。 * @param str1 短字符串 * @param str2 长字符串 */ public static boolean occurs(String str1, String str2) { Set<Character> set1 = new HashSet<>(); Set<Character> set2 = new HashSet<>(); for (int i = 0; i < str1.length(); i++) { set1.add(str1.charAt(i)); } for (int i = 0; i < str2.length(); i++) { set2.add(str2.charAt(i)); } for (Character ch : set1) { if (!set2.contains(ch)) { return false; } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.next(); String s2 = sc.next(); boolean flag = occurs(s1, s2); System.out.println(flag); } }