【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 ...
随机推荐
- lua源码学习篇二:语法分析
一步步调试,在lparser.c文件中luaY_parser函数是语法分析的重点函数,词法分析也是在这个过程中调用的.在这个过程中,用到一些数据结构,下面会详细说. Proto *luaY_parse ...
- diff()函数
1 diff()是将原来的数据减去移动后的数据. 在numpy和pandas中都能调用. pandas的调用方法: import pandas as pd df = pd.DataFrame( {'a ...
- 在Vue文件中引入外部URL链接
前言:最近做一个vueNuxt的项,没有index.html 也没有main.js项目需要引入一些外部的包,没什么技术含量只是一种思路 在vue生命钩子函数中动态创建JavaScript标签追加到HT ...
- 前端使用Git 切换分支 查看线上远程,本地切换
想要使用Git切换线上分支时先 得先查看线上分支 git branch -a //查看线上分支 git branch //查看本地分支 这是线上的分支图(当前是master) 知道有那些分支就可以进行 ...
- pycharm2018.2安装
1.官网下载安装包 https://www.jetbrains.com/pycharm/download/#section=windows (下载2018.2版本,进行破解) 2.参考其他博主安装破解 ...
- C++ Primer笔记(1)——连续读取数据、类型对应的尺寸、类型转换、字符串分行写法
这次要看看C++ Primer,这本基本上就是必读书籍了.下面的内容就是一些之前没有学过的知识的笔记. 读取数量不定的输入数据 虽然很简单,但是还是记一下: #include <iostream ...
- MySQL学习-入门语句以及增删查改
1. SQL入门语句 SQL,指结构化查询语言,全称是 Structured Query Language,是一种 ANSI(American National Standards Institute ...
- 四、Zabbix-zabbix agent部署
1.添加zabbix安装源 rpm -i http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch ...
- 【监控笔记】【2.3】扩展事件——慢查询SQL(执行超过3S的SQL)
--sql server 2008及以上才支持,2012及以上才支持GUI界面 msdn 扩展事件:点击打开链接 [1]T-SQL实现 基于 rpc_completed(远程过程调用已完成时发生) 事 ...
- P1311选择客栈
这是2011年提高组D1T2,是一个绿色的模拟题,不出所料,没写出代码来. 首先输入n个客栈的颜色和最低消费,然后根据“同颜色但不是一个客栈”以及“两个客栈之间必须有一个的最低消费<=p&quo ...