1160. Find Words That Can Be Formed by Characters(拼写单词)

1160. Find Words That Can Be Formed by Characters(拼写单词)

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.

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.
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.
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
All strings contain lowercase English letters only.

代码

java代码

package question1160;

import java.util.HashMap;
import java.util.Map;

public class Solution {

//先把字母表放到键值对map集合,key为字母,value为字母数量
//然后双重循环
//1.遍历单词表,取出单词,复制一个map集合
//   把单词转为char数组,获取最后一个元素的下标
//2.遍历char数组,将对应下标的字符作为key,用containsKey判断是否有值value,
//   如果有值value,且大于0,将当前值减一。
//   如果没有对于key的键值对,则跳出当前循环,
//判断i变量是否遍历完,如果遍历完,则总长度加上当前字符串的长度
    public int countCharacters(String[] words, String chars) {
    	char[] mchar = chars.toCharArray();
    	Map<Integer,Integer> map = 
    			new HashMap<Integer, Integer>(),tmap = null;
    	for(char c:mchar) {
    		if(map.containsKey(Integer.valueOf(c))) {
    			map.put(Integer.valueOf(c), 
    					map.get(Integer.valueOf(c))+1);
    		}else {
    			map.put(Integer.valueOf(c), 1);
    		}
    	}
    	int num = 0;
    	for(String word:words) {
			tmap = new HashMap<Integer, Integer>(map);
			char[] mword = word.toCharArray();
			int i = mword.length-1;
			for (; i >= 0; i--) {
				Integer ieg = Integer.valueOf(mword[i]);
				if (tmap.containsKey(ieg)&& tmap.get(ieg) > 0) {
					tmap.put(ieg, tmap.get(ieg) - 1);
				} else {
					break;
				}
			}
			if(i==-1) {
				num+=mword.length;
			}
    	}
    	return num;
    }
    
	public static void main(String[] args) {
		Solution solu = new Solution();
		String[] words = { "cat", "bt", "hat", "tree" };
		String chars = "atach";
		System.out.println(solu.countCharacters(words, chars));
		
		String[]  words2={"hello","world","leetcode"};
		String chars2 = "welldonehoneyr";
		System.out.println(solu.countCharacters(words2, chars2));
		
	}

}

python3代码

显然,对于一个单词 word,只要其中的每个字母的数量都不大于 chars 中对应的字母的数量,那么就可以用 chars 中的字母拼写出 word。所以我们只需要用一个哈希表存储 chars 中每个字母的数量,再用一个哈希表存储 word 中每个字母的数量,最后将这两个哈希表的键值对逐一进行比较即可。

class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        chars_cnt = collections.Counter(chars)
        ans = 0
        for word in words:
            word_cnt = collections.Counter(word)
            for c in word_cnt:
                if chars_cnt[c] < word_cnt[c]:
                    break
            else:
                ans += len(word)
        return ans
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值