题目考察的知识点是:
字符串的遍历。
题目解答方法的文字分析:
我们遍历字符串requirements,并逐个减少哈希表中对应字符的出现次数。如果在遍历requirements的过程中,发现某个字符的出现次数已经为0,或者哈希表中没有该字符,说明requirements不能由allocations中的字符构成,返回字符串"NO",否则返回"YES"。
本题解析所用的编程语言:
java语言。
完整且正确的编程代码:
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param requirements string字符串 * @param allocations string字符串 * @return string字符串 */ public String can_construct (String requirements, String allocations) { // write code here int[] rec = new int[26]; for (char c : allocations.toCharArray()) { rec[c - 'A']++; } for (char c : requirements.toCharArray()) { rec[c - 'A']--; if (rec[c - 'A'] < 0) { return "NO"; } } return "YES"; } }