leetcode:那些年我遇到过的编程题003:拼写单词

本文详细解析了LeetCode上一道关于字符串操作的编程题,通过对比数组和HashMap两种方法,探讨了如何利用字符计数来判断一个字符串是否能由另一个字符串的字符组成,提供了清晰的代码实现和思路说明。

leetcode:那些年我遇到过的编程题003

给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。

假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。

注意:每次拼写时,chars 中的每个字母都只能用一次。

返回词汇表 words 中你掌握的所有单词的 长度之和。

 

示例 1:

输入:words = ["cat","bt","hat","tree"], chars = "atach"
输出:6
解释: 
可以形成字符串 "cat""hat",所以答案是 3 + 3 = 6。
示例 2:

输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
输出:10
解释:
可以形成字符串 "hello""world",所以答案是 5 + 5 = 10。
 

提示:

1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
所有字符串中都仅包含小写英文字母

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我的思路:用一个26长度的数组分别代表26个英文字母的数目,再用另外一个数组代表word中26个英文字母的数目,彼此相减比较一下得出是否能够组合出来。

class Solution {
    public int countCharacters(String[] words, String chars) {
        int[] c = new int[26];
        int res = 0;
        for(char cchar:chars.toCharArray()){
            c[cchar - 'a']+= 1;
        }
        
        a:for(String word:words){
            int[] a = new int[26];
            for(char dchar:word.toCharArray()){
                a[dchar-'a']+= 1;
            }
            for(int i=0;i<26;i++){
                if(c[i]<a[i])
                    continue a;
            }
            res+=word.length();
        }
        return res;
    }
}

这里边主要用到的是for的另一种写法:
for(数据类型 数据名:数据集合)即以数据类型为单位,遍历数据集合。
continue回顾了一下:结束本次循环直接开始下一次循环。
continue:a 直接跳转到a:处
下面介绍另一种类似的方法,用到hashmap`

class Solution {
    public int countCharacters(String[] words, String chars) {
        int[] hash = new int[26];
        int[] temp = new int[26];
        for(char c : chars.toCharArray())
            hash[c - 97]++;
        int sum = 0;
        for(String s : words)
        {
            int i;
            for(i = 0; i < 26; i++)
                temp[i] = hash[i];
            char[] current = s.toCharArray();
            for(i = 0; i < current.length; i++)
            {
                if(temp[current[i] - 97] > 0)
                    temp[current[i] - 97]--;
                else break;
            }
            if(i == current.length)
                sum += current.length;
        }
        return sum;
    }
}

作者:tyanyonecancode
链接:https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/solution/marveljian-dan-de-xue-xi-bi-ji-1160-by-marvel_ty/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
这种方法不使用跳转和continue
turn
     */
    public int countCharacters2(String[] words, String chars) {
        /* 如果words为空或者长度为0
           如果chars为空或者长度为0
           直接返回0 */
        if (words == null || words.length == 0 || chars == null || chars.length() == 0)
            return 0;

        int spellOut = 0; // 可以拼出的字母的长度
        // boolean canSpell;    // 标识位,用来标识能否拼出
        Map<Character, Integer> wordCounter;    // words字符计数器
        Map<Character, Integer> charCounter = new HashMap<>();    // chars字符计数器

        /* 记录chars中每个字符出现的次数 */
        for (char c: chars.toCharArray()) {
            charCounter.put(c, charCounter.getOrDefault(c, 0) + 1);
        }

        /* 记录words中的每个word的每个字符的出现的次数
           并与chars计数器比较 */
        loop:  // 标签,用来处理内部循环与外部循环之间的通信,如不使用,可以在循环使用一个boolean类型的变量来判断是否符合条件
        for (String w: words) {
            /* 如果w为空或者w长度为0或者w的长度>chars的长度,不参与统计 */
            if (w == null || w.length() == 0 || w.length() > chars.length())
                continue;

            wordCounter = new HashMap<>();
            // flag = true;
            for (char c: w.toCharArray()) {
                // 判断该字符是否在chars中出现
                if (!charCounter.containsKey(c)) continue loop; // canSpell = false;
                wordCounter.put(c, wordCounter.getOrDefault(c, 0) + 1);
            }
            /* 判断每个word的字符出现次数是否至少在chars中同样出现
               即word的字符出现次数是否<=chars出现的次数 */
            for (char c: wordCounter.keySet()) {
                if (wordCounter.get(c) > charCounter.getOrDefault(c, 0))
                    continue loop;  // canSpell = false亦可;
            }
            //if (canSpell)
            spellOut += w.length();
        }
        return spellOut;
    }
}

作者:southwind_touch
链接:https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/solution/javaban-ben-xiang-xi-zhu-shi-shu-zu-yu-hashmap-by-/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

这个我写了半天老是出问题,看了下大佬的,这注释量,鬼鬼。
总结一下就是 hashmap的泛类型:Character,Integer,Double,String之类的,还有一个方法:.getOrDefault(c,default);意思是如果map中已经存在c的情况下就直接用c对应的v值,如果没有c的话put(c,defult)这么玩的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值