LeetCode 1160「拼写单词」多解法详解字符频率计数与定长数组优化【免费下载链接】leetcodeLeetcode solutions项目地址: https://gitcode.com/GitHub_Trending/leetcode1/leetcode导读本文围绕 LeetCode 1160「Find Words That Can Be Formed by Characters拼写单词」展开系统讲解如何用字符频率计数判断单词是否可由给定字符串chars拼成并给出「两遍哈希表」「单遍哈希表」「定长 26 数组哈希表」三种递进解法及完整的多语言实现。本仓库 leetcode 的 Java 版实现 与 Kotlin 版实现 与本文算法一一对应。读完本文你将掌握频率计数类字符串问题的通用套路并能在面试中根据数据规模自主选择哈希表与数组两种实现。问题定义与前置知识题目要求给定一个字符串数组words和一个字符池字符串chars统计所有「能由chars中的字符拼成」的单词返回这些单词长度之和。判定规则只有一个核心点单词中每个字符出现的次数都必须小于等于该字符在chars中出现的次数。注意这不是子序列或子串匹配顺序无关只看字符的多重集合multiset是否被包含。动手解题前你需要具备以下基础哈希表 / 字典用于统计字符频率并进行 O(1) 查找字符串遍历逐字符遍历以构建频率计数定长数组当哈希表当字符集已知且有限本题为 26 个小写字母时用int[26]替代哈希表是本题乃至很多计数类题目的关键优化手段。在仓库的 Kotlin 实现 中可以看到groupingBy { it }.eachCount()这种用标准库直接统计频率的写法本质与手写哈希表相同。解法一哈希表两遍扫描思路先遍历一遍chars建立全局频率表count然后对每个单词再遍历一遍建立该单词自己的频率表cur_word逐字符核对cur_word[c] count[c]。之所以叫「两遍」是因为对每个单词而言先完整构建其频率表第一遍再整体比较第二遍。算法步骤构建chars的频率表count对每个单词w构建单词频率表cur_word检查cur_word中每个字符的出现次数是否均不超过count中的对应值若合法将len(w)累加到结果res返回res。多语言实现class Solution: def countCharacters(self, words: List[str], chars: str) - int: count Counter(chars) res 0 for w in words: cur_word Counter(w) good True for c in cur_word: if cur_word[c] count[c]: good False break if good: res len(w) return respublic class Solution { public int countCharacters(String[] words, String chars) { MapCharacter, Integer count new HashMap(); for (char c : chars.toCharArray()) { count.put(c, count.getOrDefault(c, 0) 1); } int res 0; for (String w : words) { MapCharacter, Integer curWord new HashMap(); for (char c : w.toCharArray()) { curWord.put(c, curWord.getOrDefault(c, 0) 1); } boolean good true; for (char c : curWord.keySet()) { if (curWord.get(c) count.getOrDefault(c, 0)) { good false; break; } } if (good) { res w.length(); } } return res; } }class Solution { public: int countCharacters(vectorstring words, string chars) { unordered_mapchar, int count; for (char c : chars) { count[c]; } int res 0; for (const string w : words) { unordered_mapchar, int curWord; for (char c : w) { curWord[c]; } bool good true; for (const auto p : curWord) { if (p.second count[p.first]) { good false; break; } } if (good) { res w.size(); } } return res; } };class Solution { /** * param {string[]} words * param {string} chars * return {number} */ countCharacters(words, chars) { const count {}; for (const c of chars) { count[c] (count[c] || 0) 1; } let res 0; for (const w of words) { const curWord {}; for (const c of w) { curWord[c] (curWord[c] || 0) 1; } let good true; for (const c in curWord) { if (curWord[c] (count[c] || 0)) { good false; break; } } if (good) { res w.length; } } return res; } }public class Solution { public int CountCharacters(string[] words, string chars) { Dictionarychar, int count new Dictionarychar, int(); foreach (char c in chars) { if (!count.ContainsKey(c)) count[c] 0; count[c]; } int res 0; foreach (string w in words) { Dictionarychar, int curWord new Dictionarychar, int(); foreach (char c in w) { if (!curWord.ContainsKey(c)) curWord[c] 0; curWord[c]; } bool good true; foreach (var kvp in curWord) { int available count.ContainsKey(kvp.Key) ? count[kvp.Key] : 0; if (kvp.Value available) { good false; break; } } if (good) { res w.Length; } } return res; } }func countCharacters(words []string, chars string) int { count : make(map[rune]int) for _, c : range chars { count[c] } res : 0 for _, w : range words { curWord : make(map[rune]int) for _, c : range w { curWord[c] } good : true for c, cnt : range curWord { if cnt count[c] { good false break } } if good { res len(w) } } return res }class Solution { fun countCharacters(words: ArrayString, chars: String): Int { val count mutableMapOfChar, Int() for (c in chars) { count[c] count.getOrDefault(c, 0) 1 } var res 0 for (w in words) { val curWord mutableMapOfChar, Int() for (c in w) { curWord[c] curWord.getOrDefault(c, 0) 1 } var good true for ((c, cnt) in curWord) { if (cnt count.getOrDefault(c, 0)) { good false break } } if (good) { res w.length } } return res } }class Solution { func countCharacters(_ words: [String], _ chars: String) - Int { var count [Character: Int]() for c in chars { count[c, default: 0] 1 } var res 0 for w in words { var curWord [Character: Int]() for c in w { curWord[c, default: 0] 1 } var good true for (c, cnt) in curWord { if cnt (count[c] ?? 0) { good false break } } if good { res w.count } } return res } }impl Solution { pub fn count_characters(words: VecString, chars: String) - i32 { let mut count HashMap::new(); for c in chars.chars() { *count.entry(c).or_insert(0) 1; } let mut res 0; for w in words { let mut cur_word HashMap::new(); for c in w.chars() { *cur_word.entry(c).or_insert(0) 1; } let mut good true; for (c, cnt) in cur_word { if cnt *count.get(c).unwrap_or(0) { good false; break; } } if good { res w.len() as i32; } } res } }复杂度分析时间复杂度O(n m·k)其中 n 为chars的长度m 为单词个数k 为单词平均长度空间复杂度O(1)——因为字符集最多 26 个不同小写字母哈希表规模有常数上界。符号约定n 为chars长度m 为words中的单词数k 为每个单词的平均长度。仓库中的 Java 直观版实现 采用的就是「先建chars频率数组、再为每个单词建频率数组、最后逐下标比较」的两遍思路只是把哈希表换成了int[26]对应下面解法三。解法二哈希表单遍扫描支持提前终止思路解法一需要先完整构建每个单词的频率表再比较解法二则改为边遍历边校验遍历单词字符的同时在临时表中累加计数一旦发现某个字符的累计数超过chars中的可用数立即标记该单词非法并 break从而对非法单词实现提前终止减少不必要的遍历。算法步骤构建chars的频率表count对每个单词初始化空表cur_word逐字符遍历cur_word[c]自增若cur_word[c] count[c]标记非法并 break若合法累加len(w)到res返回res。多语言实现class Solution: def countCharacters(self, words: List[str], chars: str) - int: count Counter(chars) res 0 for w in words: cur_word defaultdict(int) good True for c in w: cur_word[c] 1 if cur_word[c] count[c]: good False break if good: res len(w) return respublic class Solution { public int countCharacters(String[] words, String chars) { MapCharacter, Integer count new HashMap(); for (char c : chars.toCharArray()) { count.put(c, count.getOrDefault(c, 0) 1); } int res 0; for (String w : words) { MapCharacter, Integer curWord new HashMap(); boolean good true; for (char c : w.toCharArray()) { curWord.put(c, curWord.getOrDefault(c, 0) 1); if (curWord.get(c) count.getOrDefault(c, 0)) { good false; break; } } if (good) { res w.length(); } } return res; } }class Solution { public: int countCharacters(vectorstring words, string chars) { unordered_mapchar, int count; for (char c : chars) { count[c]; } int res 0; for (const string w : words) { unordered_mapchar, int curWord; bool good true; for (char c : w) { curWord[c]; if (curWord[c] count[c]) { good false; break; } } if (good) { res w.size(); } } return res; } };class Solution { /** * param {string[]} words * param {string} chars * return {number} */ countCharacters(words, chars) { const count {}; for (const c of chars) { count[c] (count[c] || 0) 1; } let res 0; for (const w of words) { const curWord {}; let good true; for (const c of w) { curWord[c] (curWord[c] || 0) 1; if (curWord[c] (count[c] || 0)) { good false; break; } } if (good) { res w.length; } } return res; } }public class Solution { public int CountCharacters(string[] words, string chars) { Dictionarychar, int count new Dictionarychar, int(); foreach (char c in chars) { if (!count.ContainsKey(c)) count[c] 0; count[c]; } int res 0; foreach (string w in words) { Dictionarychar, int curWord new Dictionarychar, int(); bool good true; foreach (char c in w) { if (!curWord.ContainsKey(c)) curWord[c] 0; curWord[c]; int available count.ContainsKey(c) ? count[c] : 0; if (curWord[c] available) { good false; break; } } if (good) { res w.Length; } } return res; } }func countCharacters(words []string, chars string) int { count : make(map[rune]int) for _, c : range chars { count[c] } res : 0 for _, w : range words { curWord : make(map[rune]int) good : true for _, c : range w { curWord[c] if curWord[c] count[c] { good false break } } if good { res len(w) } } return res }class Solution { fun countCharacters(words: ArrayString, chars: String): Int { val count mutableMapOfChar, Int() for (c in chars) { count[c] count.getOrDefault(c, 0) 1 } var res 0 for (w in words) { val curWord mutableMapOfChar, Int() var good true for (c in w) { curWord[c] curWord.getOrDefault(c, 0) 1 if (curWord[c]!! count.getOrDefault(c, 0)) { good false break } } if (good) { res w.length } } return res } }class Solution { func countCharacters(_ words: [String], _ chars: String) - Int { var count [Character: Int]() for c in chars { count[c, default: 0] 1 } var res 0 for w in words { var curWord [Character: Int]() var good true for c in w { curWord[c, default: 0] 1 if curWord[c]! (count[c] ?? 0) { good false break } } if good { res w.count } } return res } }impl Solution { pub fn count_characters(words: VecString, chars: String) - i32 { let mut count HashMap::new(); for c in chars.chars() { *count.entry(c).or_insert(0) 1; } let mut res 0; for w in words { let mut cur_word HashMap::new(); let mut good true; for c in w.chars() { *cur_word.entry(c).or_insert(0) 1; if cur_word[c] *count.get(c).unwrap_or(0) { good false; break; } } if good { res w.len() as i32; } } res } }复杂度分析时间复杂度O(n m·k)平均情况下因提前终止而更省空间复杂度O(1)字符种类上限为 26。符号约定同上n 为chars长度m 为单词数k 为单词平均长度。与解法一的取舍解法二的优势在于尽早发现非法单词对于「cat」这样的短单词影响不大但对于长且明显超额的单词如由 1000 个z组成而chars里只有一个z在遍历第 2 个字符时就能终止避免继续构建整张表。代价是每处理一个字符都要多做一次比较常数略大。两者渐近复杂度相同面试中二者皆可重点是要能讲清「两遍」与「单遍 提前终止」的差异。解法三定长 26 数组哈希表最优实践思路题目约束字符只可能是小写字母因此可以用固定大小int[26]数组替代哈希表count[c - a]即字符c的频率。数组下标运算比哈希函数更快、对 CPU 缓存更友好且避免了哈希碰撞的开销。校验时采用递减法直接用chars的频率数组逐个扣减单词字符一旦某位置变成负数说明该单词非法每个单词处理完后用保存的原始副本org重置数组。算法步骤创建长度 26 的数组count遍历chars填充频率复制一份count到org用于每轮重置对每个单词逐字符执行count[c - a]--若某次递减后为负数标记非法并 break若合法累加len(w)到res将count重置为org供下一个单词使用返回res。多语言实现class Solution: def countCharacters(self, words: List[str], chars: str) - int: count [0] * 26 for c in chars: count[ord(c) - ord(a)] 1 org count[:] res 0 for w in words: good True for c in w: i ord(c) - ord(a) count[i] - 1 if count[i] 0: good False break if good: res len(w) for i in range(26): count[i] org[i] return respublic class Solution { public int countCharacters(String[] words, String chars) { int[] count new int[26]; for (char c : chars.toCharArray()) { count[c - a]; } int[] org count.clone(); int res 0; for (String w : words) { boolean good true; for (int i 0; i w.length(); i) { int j w.charAt(i) - a; count[j]--; if (count[j] 0) { good false; break; } } if (good) { res w.length(); } for (int i 0; i 26; i) { count[i] org[i]; } } return res; } }class Solution { public: int countCharacters(vectorstring words, string chars) { vectorint count(26, 0); for (char c : chars) { count[c - a]; } vectorint org count; int res 0; for (string w : words) { bool good true; for (char c : w) { int i c - a; count[i]--; if (count[i] 0) { good false; break; } } if (good) { res w.length(); } for (int i 0; i 26; i) { count[i] org[i]; } } return res; } };class Solution { /** * param {string[]} words * param {string} chars * return {number} */ countCharacters(words, chars) { const count new Array(26).fill(0); for (let c of chars) { count[c.charCodeAt(0) - a.charCodeAt(0)]; } const org [...count]; let res 0; for (let w of words) { let good true; for (let c of w) { const i c.charCodeAt(0) - a.charCodeAt(0); count[i]--; if (count[i] 0) { good false; break; } } if (good) { res w.length; } for (let i 0; i 26; i) { count[i] org[i]; } } return res; } }public class Solution { public int CountCharacters(string[] words, string chars) { int[] count new int[26]; foreach (char c in chars) { count[c - a]; } int[] org (int[])count.Clone(); int res 0; foreach (string w in words) { bool good true; foreach (char c in w) { int i c - a; count[i]--; if (count[i] 0) { good false; break; } } if (good) { res w.Length; } for (int i 0; i 26; i) { count[i] org[i]; } } return res; } }func countCharacters(words []string, chars string) int { count : make([]int, 26) for _, c : range chars { count[c-a] } org : make([]int, 26) copy(org, count) res : 0 for _, w : range words { good : true for _, c : range w { i : c - a count[i]-- if count[i] 0 { good false break } } if good { res len(w) } copy(count, org) } return res }class Solution { fun countCharacters(words: ArrayString, chars: String): Int { val count IntArray(26) for (c in chars) { count[c - a] } val org count.copyOf() var res 0 for (w in words) { var good true for (c in w) { val i c - a count[i]-- if (count[i] 0) { good false break } } if (good) { res w.length } for (i in 0 until 26) { count[i] org[i] } } return res } }class Solution { func countCharacters(_ words: [String], _ chars: String) - Int { var count Int let aValue Int(Character(a).asciiValue!) for c in chars { count[Int(c.asciiValue!) - aValue] 1 } let org count var res 0 for w in words { var good true for c in w { let i Int(c.asciiValue!) - aValue count[i] - 1 if count[i] 0 { good false break } } if good { res w.count } count org } return res } }impl Solution { pub fn count_characters(words: VecString, chars: String) - i32 { let mut count [0i32; 26]; for c in chars.bytes() { count[(c - ba) as usize] 1; } let org count; let mut res 0; for w in words { let mut good true; for c in w.bytes() { let i (c - ba) as usize; count[i] - 1; if count[i] 0 { good false; break; } } if good { res w.len() as i32; } count org; } res } }复杂度分析时间复杂度O(n m·k)其中 n 为chars长度m 为单词数k 为单词平均长度空间复杂度O(1)——固定 26 长度的数组不随输入规模增长。为什么数组版本更优从实现层面看数组方案有三点收益无哈希开销c - a是纯算术运算不涉及哈希函数计算与碰撞处理缓存友好26 个 int 完全驻留于 L1 缓存访问模式是顺序或近似顺序的代码意图清晰count[c - a]--配合「小于 0 即非法」的判断直观表达消耗字符的过程。仓库的 Java 实现 提供了该思路的直观版为每个单词单独建int[26]频率数组再与chars的数组逐下标比较arrS[i] arrChars[i]即非法。它没有使用递减 重置的技巧但两者在复杂度上完全等价适合作为最容易写对的第一版。Kotlin 版中则有IntArray.canMake的模块化写法把「比较两个频率数组」抽成独立函数增强了可读性。常见误区误区一修改了原始的字符计数表却未恢复逐词校验时必须使用chars频率表的新鲜副本或在校验后将其重置。常见错误是直接递减原始count却不恢复导致后续单词可用的字符数被错误减少输出偏小。解法三中用org副本每轮重置正是为了避免这一点如果采用每个单词新建频率数组再比较的方式如仓库 Java 直观版则天然规避该问题。误区二只检查字符存在而不检查频率判断单词能否拼成时只确认每个字符都出现在chars里是不够的必须验证单词中每个字符的频率不超过chars中该字符的频率。例如chars ab时aab虽然每个字符都出现在chars中但a需要 2 个而chars只有 1 个因此aab无法拼成。实战要点总结维度两遍哈希表单遍哈希表定长 26 数组核心思想先建词频表再整体比较边遍历边校验超额即停递减计数负数即非法提前终止不支持支持支持哈希/下标开销有有无纯算术空间O(1)≤26 键O(1)≤26 键O(1)固定 26适用场景通用字符集通用字符集小写字母限定三个解法的时间复杂度均为 O(n m·k)。当题目明确限定字符集如仅小写字母、仅数字时优先选用定长数组当字符集未知或很大如 Unicode时退回哈希表方案。本题的判定逻辑——逐字符频率比较——也是「Valid Anagram」「Ransom Note」等一批计数类题目的共同基础掌握好数组与哈希表的互换即可一通百通。参考实现索引本文对应题目详解articles/find-words-that-can-be-formed-by-characters.mdJava 实现数组两遍比较版java/1160-find-words-that-can-be-formed-by-characters.javaKotlin 实现含模块化与函数式短写三版kotlin/1160-find-words-that-can-be-formed-by-characters.kt项目全局导航README.md【免费下载链接】leetcodeLeetcode solutions项目地址: https://gitcode.com/GitHub_Trending/leetcode1/leetcode创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考