【leetcode】1160. Find Words That Can Be Formed by Characters
题目如下:
You are given an array of strings
words
and a stringchars
.A string is good if it can be formed by characters from
chars
(each character can only be used once).Return the sum of lengths of all good strings in
words
.Example 1:
Input: words = ["cat","bt","hat","tree"], chars = "atach"
Output: 6
Explanation:
The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6.Example 2:
Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr"
Output: 10
Explanation:
The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10.Note:
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
- All strings contain lowercase English letters only.
解题思路:很简单的题目,chars中每个字符出现的次数要大于等于word中每个字符出现的次数,即表示可以组成这个word。
代码如下:
class Solution(object):
def countCharacters(self, words, chars):
"""
:type words: List[str]
:type chars: str
:rtype: int
"""
res = 0
for word in words:
flag = True
for i in word:
if word.count(i) > chars.count(i):
flag = False
break
if flag: res += len(word)
return res
【leetcode】1160. Find Words That Can Be Formed by Characters的更多相关文章
- 【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetco ...
- 【Leetcode_easy】1160. Find Words That Can Be Formed by Characters
problem 1160. Find Words That Can Be Formed by Characters solution class Solution { public: int coun ...
- 【LeetCode】159. Longest Substring with At Most Two Distinct Characters
Difficulty: Hard More:[目录]LeetCode Java实现 Description Given a string S, find the length of the long ...
- 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...
- 【leetcode】395. Longest Substring with At Least K Repeating Characters
题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
随机推荐
- 阶段3 1.Mybatis_07.Mybatis的连接池及事务_5 mybatis中使用poolead配置连接的原理分析
idelConnection是空闲的链接 idelConnection就是ArrayList的数组 如果没有空闲的就new一个 新的connection 一个空闲池,一个活动的池,一个链接过来.空闲池 ...
- 阶段3 1.Mybatis_01.Mybatis课程介绍及环境搭建_05.mybatis环境搭建-前期准备
视频中右侧没有勾选 直接finish 用下面的sql里面的一些表来实现今天的功能 只需要用到里面的user表. 这是之前已经建好的数据库 把表都删除掉,用sql语句去创建表和表内的记录,最终的结果: ...
- 六:flask-自定义URL转换器
flask进行url参数匹配的时候,是继承的werkzeug.routing.BaseConverter库进行重写的 导入看源码 里面有所有的URL参数数据类型的判断 也就是说,可以继承过后实现自己的 ...
- idea把java web项目打成war包
1.新建artifacts 2.设置你的目录内容路径 3.找到项目web或webapp的路径 4.可以直接添加已经跑通的项目 5.配置完成点OK 6.编译打成war包 7.点击编译结束打完收工 8.找 ...
- nginx rewrite + 排错方法 + server_name 172.19.134.43
upstream space.two.cn { ip_hash; #ip hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题. serve ...
- Canvas入门01-基础知识
定义一个canvas,直接在Html中使用canvas便签即可. <!DOCTYPE html> <html lang="en"> <head> ...
- django 的 一对多的关系
USERINFO 用户详情表 USERTYPE 用户类别表 UserType是父表,UserInfo是子表, user_type 是 关联字段 就是新增资源的时候,又对数据库重新查询一遍,太消耗资源了 ...
- @Scheduled(cron = "0/5 * * * * *")将时间改为配置
有两种方法:第一种当然你可以把Scheduled写到xml文件中进行配置. 第二种在你的类前面添加 此处讲解第二种写法 第二种在你的类前面添加@PropertySource("classpa ...
- 红帽学习笔记[RHCSA] 第六课[进程、服务相关]
第六课 进程 进程:已经启动的可执行程序的运行中的实例.每个进程都有自己的地址空间,并占用了一定的系统资源. 如何产生一个进程 执行程序或命令 计划任务 在终端中对进程管理 运行一个前台进程 [roo ...
- 一些重温CSS需要注意的小细节
<!-- CSS是用于描述页面展示的语言 字体.颜色.大小.间距,将内容分为多列 或者简单的动画及其他的装饰效果 决定了长啥样 html房子的骨架 css负责装修 怎么装修一个房子呢? 首先就是 ...