目录
一、Map的应用篇
乒 乓球筐
小白代码
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String A = in.next();
String B = in.next();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < A.length(); ++i) {
if (!map.containsKey(A.charAt(i))) {
map.put(A.charAt(i), 1);
}
else {
int temp = map.get(A.charAt(i));
map.put(A.charAt(i), temp + 1);
}
}
int[] arrB = new int[100];
for (int i = 0; i < B.length(); ++i) {
// System.out.println(B.charAt(i) - 'A');
arrB[B.charAt(i) - 'A']++;
}
int flag = 1;
for (int i = 0; i < B.length(); ++i) {
if (map.containsKey(B.charAt(i))) {
int j = B.charAt(i) - 'A';
if (arrB[j] <= map.get(B.charAt(i))) {
// 符合要求什么也不做
}
else {
// 不符合要求就改变flag
flag = 0;
}
}
else {
flag = 0;
}
}
if (flag == 1) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
}
扩展做法
// write your code here
// write your code here
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNext()) { // 注意 while 处理多个 case
String temp = in.next();
StringBuilder A = new StringBuilder(temp);
String B = in.next();
char[] find = B.toCharArray();
int flag = 1;
for (char ch : find) {
// 不等于-1,说明该字符在A中
int index = A.indexOf(String.valueOf(ch));
if (index != -1) {
// 题目要求的是B中的字符在A中都有,并且在B中的数量不多于在A中的数量
// 我们就在遍历B中的过程,逐步的删除A中与B中相同的字符,如果从头到尾B中的字符都在A中
// 删除就是防止A中虽然有B中的字符,但没有B中的字符数量多
A.deleteCharAt(index);
}
else {
flag = 0;
}
}
if (flag == 1) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
}