【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 ...
随机推荐
- Unity3D 可空值类型 Nullable
值类型的变量永远不会变null,因为值类型是其本身不会变成null.引用类型可变成null,内存会全部使用0来表示null,因为这种开销会降低,仅仅需要将一块内存清除. 表示一些空值的方案: 1.使用 ...
- 快速编写 <a> ————CSS3
a{ text-decoration:none; } a:link{ color:white; } a:visited { color:white; } a:hover { color:blue; } ...
- 什么是Shell?Shell脚本基础知识详细介绍
这篇文章主要介绍了什么是Shell?Shell脚本基础知识介绍,本文是一篇Shell脚本入门文章,在本文你可学到什么是Shell.有多少种Shell.一个Shell脚本代码实例,需要的朋友可以参考下 ...
- 无法获得锁 /var/lib/dpkg/lock-frontend - open (11: 资源暂时不可用)
ubuntu更新软件时 apt-get upgrade 遇到 E: 无法获得锁 /: 资源暂时不可用) E: Unable to acquire the dpkg frontend lock (/va ...
- jQuery 获取设置图片 src 的路径
1.获取: $('#img')[0].src 2.设置:$('#img').attr("src", data.CodeUrl);
- 精读《What's new in javascript》
1. 引言 本周精读的内容是:Google I/O 19. 2019 年 Google I/O 介绍了一些激动人心的 JS 新特性,这些特性有些已经被主流浏览器实现,并支持 polyfill,有些还在 ...
- @Scheduled(cron = "0/5 * * * * *")将时间改为配置
有两种方法:第一种当然你可以把Scheduled写到xml文件中进行配置. 第二种在你的类前面添加 此处讲解第二种写法 第二种在你的类前面添加@PropertySource("classpa ...
- python 开启进程两种方法 multiprocessing模块 介绍
一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu\_count\(\)查看),在python中大部分情况需要使用多进 ...
- Sequential game
Sequential game Problem Description Sequential detector is a very important device in Hardware exper ...
- P1190排队接水
这是2010年普及组第二题(话说我怎么只做普及组的题?因为太蒻). 这道题可以用搜索或者模拟来做,果断编了几组例子去分析来模拟,利用了贪心的思想得出:每次比较每个水龙头的空闲时间点,谁早就放到谁那里, ...