当前位置:   article > 正文

[leetcode]1160. 拼写单词_有一个字符串数组 words 和一个字符串 chars. 假如可以用 chars 中的字母拼写出 w

有一个字符串数组 words 和一个字符串 chars. 假如可以用 chars 中的字母拼写出 w
  • 个人博客:https://javaniuniu.com/
  • 难度:简单
  • 本题涉及算法:哈希表记数
  • 思路:哈希表记数
  • 类似题型:-

题目 1160. 拼写单词

给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars
假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
注意:每次拼写时,chars 中的每个字母都只能用一次。
返回词汇表 words 中你掌握的所有单词的 长度之和

示例

示例 1:
输入:words = ["cat","bt","hat","tree"], chars = "atach"
输出:6
解释:
可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。
  • 1
  • 2
  • 3
  • 4
示例 2:
输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
输出:10
解释:
可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
  • 1
  • 2
  • 3
  • 4
提示:
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
所有字符串中都仅包含小写英文字母
  • 1
  • 2
  • 3

解题思路

  • 统计字母出现次数
  • 两个哈希表的键值对逐一进行比较即可

友情提示:

遇到有提示字符串仅包含小写(或者大写)英文字母的题,
都可以试着考虑能不能构造长度为26的每个元素分别代表一个字母的数组,来简化计算
对于这道题,用数组c来保存字母表里每个字母出现的次数
如法炮制,再对词汇表中的每个词汇都做一数组w,比较数组w与数组c的对应位置

  • 如果w中的都不大于c,就说明该词可以被拼写出,长度计入结果
  • 如果w其中有一个超过了c,则说明不可以被拼写,直接跳至下一个(这里用到了带label的continue语法)

代码

python
class Solution(object):
    def countCharacters(self, words, chars):
        """
        :type words: List[str]
        :type chars: str
        :rtype: int
        """
        res = 0
        for word in words:
            # for i in word:
            #     if word.count(i)<=chars.count(i):
            #         flag = 1
            #         continue
            #     else:
            #         flag = 0
            #         break
            # if flag == 1:
            #     res += len(word)
            if all(word.count(i)<=chars.count(i) for i in word):
                res += len(word)
        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
java一
public class Solution {
    public int countCharacters(String[] words, String chars) {
        int[] c = new int[26];
        for(char cc:chars.toCharArray()) {
            c[(int)cc-'a']+=1;
        }
        int res = 0;
        a: for(String word:words) {
            int[] w = new int[26];
            for(char ww:word.toCharArray()) {
                w[(int)ww-'a']+=1;
            }
            for(int i=0;i<26;i++) {
                if(w[i]>c[i]) {
                    continue a;
                }
            }
            res += word.length();
        }
        return res;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
java二
class Solution {
    public int countCharacters(String[] words, String chars) {
        int[] abc = new int[26];
        for (char ch : chars.toCharArray()) {
            int index = ch - 'a';
            abc[index] += 1;
        }
        int sumlen = 0;
        a:
        for (String word : words) {
            int[] sub = abc.clone();// 注意这里 需要用拷贝,如果int[] sub = abc 指定的是同一个对象,
            int len = 0;
            for (char cha : word.toCharArray()) {
                int c = cha - 'a';
                if (sub[c] == 0) {
                    len = 0;
                    continue a;
                } else {
                    len += 1;
                    sub[c] -= 1;
                }
            }
            sumlen += len;
        }

        return sumlen;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/705302
推荐阅读
相关标签
  

闽ICP备14008679号