题目如下:

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. python学习之requests基础

    学习地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html#id2 使用requests发送网络请求 一.导入requ ...

  2. PHP 验证香港身份证ID号

    /**check hk idcard$id = "K548653A";$xx= check_hkid($id);var_dump($xx);**/function check_hk ...

  3. IDEA激活—免费永久激活(lookdiv.com)

    网址: http://lookdiv.com/ 钥匙就是网址 钥匙:lookdiv.com 亲测有效!非常好用.

  4. (转载)深入解析String#intern

    本文转载自:深入解析String#intern 引言 在 JAVA 语言中有8中基本类型和一种比较特殊的类型String.这些类型为了使他们在运行过程中速度更快,更节省内存,都提供了一种常量池的概念. ...

  5. 关于Goroutine与Channel

    关于Goroutine的原理 原理上的内容比较多,比如goroutine启动的时候要执行哪些相关的操作,一点一点的补充一下. channel的基本原理 channel是go语言中的特殊的机制,既可以同 ...

  6. 红帽学习笔记[RHCSA] 第六课[进程、服务相关]

    第六课 进程 进程:已经启动的可执行程序的运行中的实例.每个进程都有自己的地址空间,并占用了一定的系统资源. 如何产生一个进程 执行程序或命令 计划任务 在终端中对进程管理 运行一个前台进程 [roo ...

  7. JS 数组的常用方法详解归纳之改变原数组方法

    shift() 把数组的第一个元素从其中删除,并返回第一个元素的值, 如果数组是空的,那么 shift() 方法将不进行任何操作,返回 undefined 值.请注意,该方法不创建新数组,而是直接修改 ...

  8. CentOS 7.6 RPM 方式安装Oracle19c 后 使用 systemd 的方式设置开机自动启动Oracle数据库

    1. 方法简介: 使用systemd 来进行 oracle数据库的启动和关闭操作. 使用的脚本为 lsnrctl和dbstart 2. 修改事项. 需要先修改一下 oracle 的启动脚本配置: vi ...

  9. 【源码解读】pix2pix(一):训练

    源码地址:https://github.com/mrzhu-cool/pix2pix-pytorch 相比于朱俊彦的版本,这一版更加简单易读 训练的代码在train.py,开头依然是很多代码的共同三板 ...

  10. 02.AutoMapper 之扁平化(Flattening)

    https://www.jianshu.com/p/65099590c930   扁平化(Flattening) 对象映射器的常见用法是将一个复杂对象模型扁平化为一个简单模型.例如您有一个以下复杂模型 ...