拼写单词(leetcode 1160)
文章目录
- 1.问题描述
- 2.难度等级
- 3.热门指数
- 4.解题思路
- 5.实现示例
- 5.1 C++
- 5.2 Golang
- 参考文献
1.问题描述
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。
假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
注意:每次拼写(指拼写词汇表中的一个单词)时,chars 中的每个字母都只能用一次。
返回词汇表 words 中你掌握的所有单词的长度之和。
示例 1:
输入:words = [“cat”,“bt”,“hat”,“tree”], chars = “atach”
输出:6
解释: 可以形成字符串 “cat” 和 “hat”,所以答案是 3 + 3 = 6。
示例 2:
输入:words = [“hello”,“world”,“leetcode”], chars = “welldonehoneyr”
输出:10
解释:可以形成字符串 “hello” 和 “world”,所以答案是 5 + 5 = 10。
提示:
1 <= words.length <= 1000。
1 <= words[i].length, chars.length <= 100。
所有字符串中都仅包含小写英文字母。
2.难度等级
easy。
3.热门指数
★★★★☆
出题公司:道通智能。
4.解题思路
显然,对于一个单词 word,只要其中的每个字母的数量都不大于 chars 中对应的字母的数量,那么就可以用 chars 中的字母拼写出 word。所以我们只需要用一个哈希表存储 chars 中每个字母的数量,再用一个哈希表存储 word 中每个字母的数量,最后将这两个哈希表的键值对逐一进行比较即可。
复杂度分析:
时间复杂度:O(n),其中 n 为所有字符串的长度和。我们需要遍历每个字符串,包括 chars 以及数组 words 中的每个单词。
空间复杂度:O(S),其中 S 为字符集大小,在本题中 S 的值为 26(所有字符串仅包含小写字母)。程序运行过程中,最多同时存在两个哈希表,使用的空间均不超过字符集大小 S,因此空间复杂度为 O(S)。
5.实现示例
5.1 C++
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;int countWordLen(const vector<string> &words, string chars) {unordered_map<char, int> charToCnt;for (char c : chars) {charToCnt[c]++;}int wordLen = 0;for (string word : words) {unordered_map<char, int> m;for (char c : word) {m[c]++;}bool cotain = true;for (char c : word) {if (charToCnt[c] < m[c]) {cotain = false;break;}}if (cotain) {wordLen += word.size();}}return wordLen;
}int main() {auto n = countWordLen(vector<string>{"cat", "bt", "hat", "tree"}, "atach");cout << n << endl;n = countWordLen(vector<string>{"hello", "world", "leetcode"}, "welldonehoneyr");cout << n << endl;
}
运行输出:
6
10
5.2 Golang
package mainimport ("fmt"
)func countWordLen(words []string, chars string) int {// 统计字母表中字母出现的次数。charToNum := make(map[rune]int, len(chars))for _, c := range chars {v, _ := charToNum[c]v++charToNum[c] = v}// 可拼写单词总长度。var wordLen intfor _, s := range words {m := make(map[rune]int, len(s))for _, c := range s {v, _ := m[c]v++m[c] = v}contain := truefor k, v := range m {if charToNum[k] < v {contain = falsebreak}}if contain {wordLen += len(s)}}return wordLen
}func main() {n := countWordLen([]string{"cat", "bt", "hat", "tree"}, "atach")fmt.Println(n)n = countWordLen([]string{"hello", "world", "leetcode"}, "welldonehoneyr")fmt.Println(n)
}
运行输出:
6
10
参考文献
1160.拼写单词 - leetcode
拼写单词(leetcode 1160)相关推荐
- 【LeetCode】1160. 拼写单词(C++)
1160. 拼写单词(C++) 1 题目描述 2 示例描述 2.1 示例 1 2.2 示例 2 3 解题提示 4 解题思路 5 源码详解(C++) 1 题目描述 给你一份『词汇表』(字符串数组) wo ...
- LeetCode 拼写单词
文章目录 拼写单词 题目 解题思路 代码实现 实现结果 拼写单词 题目来源:https://leetcode-cn.com/problems/find-words-that-can-be-formed ...
- 1160. 拼写单词
2020-03-17 1.题目描述 拼写单词 2.解析 直接进行搜索即可 3.代码 #include <iostream> #include <cstring> #includ ...
- 伍六七带你学算法 入门篇-拼写单词
力扣解题,每日一题 1160. 拼写单词 难度- 简单 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 w ...
- 拼写单词 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符
力扣1160. 拼写单词 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字 ...
- LeetCode 1160. 拼写单词
文章目录 1. 题目 2. 解题 1. 题目 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words ...
- LeetCode简单题之拼写单词
题目 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认 ...
- Leetcode--1160.拼写单词(Java)
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌 ...
- leetcode 1160 python
题目要求: https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/ python中all函数用法: ...
最新文章
- Linux那些事儿 之 戏说USB(27)设备的生命线(十)
- 计算机办公应用适合什么工作,有什么软件堪称办公神器,让你每天的工作轻松不累?...
- 在linux中安装mysql时遇到的问题
- 全连接where 取最新日期_SQL学习笔记(4)——集合、连接与窗口函数
- Python Revisited Day 07 (文件处理)
- java builder pool_每周10道Java面试题:String, String Pool, StringBuilder
- L1-078 吉老师的回归 (15 分)-PAT 团体程序设计天梯赛 GPLT
- 【LeetCode】【字符串】题号:*423. 从英文中重建数字
- cve20190708补丁的kb名称_2019-6微软安全更新补丁KB4503269
- iOS总结-Runtime篇之黑魔法Method Swizzling的滥用会有危险吗
- C语言中excit函数,2010年12月英语四级全真预测试卷及答案解析(4)
- 智慧城市,离我们还有多远?
- BZOJ4079 : [Wf2014]Pachinko
- jQuery基础二DOM操作
- 从零开始在虚拟机下安装Ubuntu
- 【考研经验】19南京大学考研经验帖,软工从入门到入土
- myeclipse中文界面改颜色_图文设计颜色参考
- 实弹影像报靶实际训练中的应用
- 前端处理文件下载时,提示token失效
- OC如何快速将数组,int数值,long数值转NSData