WUSTOJ 1321: Alphabet Cookies(Java)字符统计
Description
Kitty likes cookies very much, and especially the alphabet cookies. Now, she get some alphabet cookies, and she wants to select some of them to spell some words.
The easy task for you, is to determine that whether she can spell the word she wants.
Input
The input contains several test cases.
Each test case contains an integer N ( 0 < N ≤ 100 ) in a line.
And followed a line with N capital letters, means the cookies she has.
Then the last line is a word with capital letters. And the word’s length isn’t more than N.
Output
One word for each test case. If she can spell the word by these letters, please output “Yes”, nor output “No”.
Sample Input
7
ARDHPYP
HAPPY
6
ARDHPY
HAPPY
Sample Output
Yes
No
代码
/**
* Time 258ms
* @author wowpH
* @version 1.0
* @date 2019年6月24日下午12:39:59
* Environment: Windows 10
* IDE Version: Eclipse 2019-3
* JDK Version: JDK1.8.0_112
*/
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(new InputStreamReader(System.in));
while (sc.hasNext()) {
int N = sc.nextInt();
String cookies = sc.next();
String word = sc.next();
int[] lettersNum = new int[26];
for (int i = cookies.length() - 1; i >= 0; --i) {
++lettersNum[cookies.charAt(i) - 'A'];// 统计每个字符的个数
}
int i;
for (i = word.length() - 1; i >= 0; --i) {
if (lettersNum[word.charAt(i) - 'A'] <= 0) {// 当前字符不够,退出
break;
}
--lettersNum[word.charAt(i) - 'A'];// 够,个数减1
}
if (i >= 0) {
System.out.println("No");
} else {
System.out.println("Yes");
}
}
sc.close();
}
}