当前位置: 代码迷 >> 综合 >> 1160. 拼写单词data=[collections.Counter(i) for i in words]
  详细解决方案

1160. 拼写单词data=[collections.Counter(i) for i in words]

热度:66   发布时间:2023-12-24 21:29:04.0

在这里插入图片描述

class Solution {
    
public:// 判断c是否在chars 中存在bool strfind(string c,string chars){
    for(int i=0;i<c.size();i++){
    if(chars.find(c[i],0)!=-1){
    chars.erase(chars.find(c[i],0),1);}else return false;}return true;}int countCharacters(vector<string>& words, string chars) {
    int res=0;for(int i=0;i<words.size();i++){
    if(strfind(words[i],chars)){
    res+=words[i].size();}}return res;}
};
class Solution:def countCharacters(self, words: List[str], chars: str) -> int:data=[collections.Counter(i) for i in words]cnt=collections.Counter(chars)res=0for i, w in enumerate(data):for j,c in w.items():if cnt[j]<c:breakelse :res+=len(words[i])return res
  相关解决方案