题解HJ81 | #字符串字符匹配#
字符串字符匹配
https://www.nowcoder.com/practice/22fdeb9610ef426f9505e3ab60164c93
import java.util.HashSet; import java.util.Scanner; import java.util.Set; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); String s2 = in.nextLine(); Set<Character> set = new HashSet<>(); for (int i = 0; i < s1.length(); i++) { set.add(s1.charAt(i)); } for (int i = 0; i < s2.length(); i++) { set.remove(s2.charAt(i)); } System.out.println(set.size() == 0); } }
用Set走捷径!把短字符串的字符加入到Set中,把长字符的字符从Set中剔除,如果最后Set为空,则满足条件。