题目如下:

You are given an array of strings words and a string chars.

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. 1 <= words.length <= 1000
  2. 1 <= words[i].length, chars.length <= 100
  3. 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的更多相关文章

  1. 【LeetCode】1160. Find Words That Can Be Formed by Characters 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典统计 日期 题目地址:https://leetco ...

  2. 【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 ...

  3. 【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 ...

  4. 【LeetCode】395. Longest Substring with At Least K Repeating Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/longest- ...

  5. 【leetcode】395. Longest Substring with At Least K Repeating Characters

    题目如下: 解题思路:题目要找出一段连续的子串内所有字符出现的次数必须要大于k,因此出现次数小于k的字符就一定不能出现,所以就可以以这些字符作为分隔符分割成多个子串,然后继续对子串递归,找出符合条件的 ...

  6. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  7. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  8. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. Jmeter接口测试系列之测试用例编写和调用

    在使用Jmeter进行接口测试时,首先需要根据接口定义,编写响应的接口测试用例,在编写接口测试用例时,我们根据测试的侧重点不同,使用不同的方式编译测试用例. 一种是:整个请求参数作为一个变量,进行测试 ...

  2. 一个ETF基金经理的心路历程

    简介: 鹏华沪深300ETF拟任基金经理崔俊杰先生,金融工程专业管理学硕士,5年证券基金从业经验.2008年7月加盟鹏华基金管理有限公司,历任产品规划部产品设计师.量化投资部量化研究员,先后从事产品设 ...

  3. IIS Express 使用方法

    配置文件位置: "%userprofile%\My Documents\IISExpress\config\applicationhost.config" 站点配置节: <s ...

  4. Chapter02 第二节 语句和变量

    2.2 C++语句 2.11 声明语句和变量 示例程序: // carrots.cpp #include <bits/stdc++.h> using namespace std; int ...

  5. Java程序的运行过程,以及Java为什么能够跨平台

    Java程序运行机制  Java的运行主要分两步:先编译再解释执行 (1)先通过“编译器”将Java源程序(.java)编译成Java字节码文件(.class) (2)通过不同的虚拟机(JVM)将字节 ...

  6. Linux-SSH远程管理服务实战

    figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...

  7. linux 系统目录权限

    小结: 目录的读.写.执行权限 1. 可读r:表示具有浏览目录 下面文件及子目录的权限 ls dir 1)如果没有x权限,则不能进到目录,既无法执行cd dir 2)如果没有x权限,ls列表时可以看到 ...

  8. 分享一个linux系统中循环遍历两个数组内容,并输出数组中的不同内容的shell脚本

    cat diffarray.sh #!/bin/bash arry_list1=(1 2 3 4 5 6 7 8 9) arry_list2=(3 5 8) declare -a diff_list ...

  9. 首次全备及事务备份对数据库的影响,2014 SpexSql log评估版探索

    参考:https://www.cnblogs.com/gered/p/9882367.html 关键词:解析事务日志 新建数据库test3,然后查看日志文件,382行记录 SELECT min([Be ...

  10. Linux下安装tomcat与配置

    准备工作:将下载好的tomcat 9.0上传到自己的阿里云服务器(推荐根目录下) 附下载地址:https://archive.apache.org/dist/tomcat/tomcat-9/v9.0. ...