1 题目
2 代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class B1039 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] input = br.readLine().toCharArray();
char[] res = br.readLine().toCharArray();
int[] hash = new int[123];
for (int i = 0; i < input.length; i++) {
hash[input[i]]++;
}
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < res.length; i++) {
set.add(res[i]);
hash[res[i]]--;
}
int num = 0;
Iterator<Character> iterator = set.iterator();
while (iterator.hasNext()) {
int index = iterator.next();
if (hash[index] < 0)
num += Math.abs(hash[index]);
}
if (num == 0)
System.out.println("Yes " + (input.length - res.length));
else
System.out.println("No " + num);
}
}
3 要点
(1)利用Hash表保存两个字符串中字符的个数,如果后续num大于0,说明缺少珠子;反之,可以购买,两个字符串长度相减即可。