首页 > 其他分享 >[LeetCode] 1160. Find Words That Can Be Formed by Characters

[LeetCode] 1160. Find Words That Can Be Formed by Characters

时间:2023-12-03 09:03:25浏览次数:26  
标签:1160 Words int word chars Formed 单词 words letters

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.

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.

Constraints:
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
words[i] and chars consist of lowercase English letters.

拼写单词。

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

思路

用一个长度为26的 int array 统计字母表中每个不同字母的出现次数,然后遍历词汇表中的每个单词,看看字母表中的字母是否足够组成当前的单词,如果能组成,则将当前单词的长度累加到结果。

复杂度

时间O(mn) - m 个单词,单词平均长度为 n
空间O(m) - 要将长度为26的 int array复制 m 次

代码

Java实现

class Solution {
    public int countCharacters(String[] words, String chars) {
        int[] map = new int[26];
        for (char c : chars.toCharArray()) {
            map[c - 'a']++;
        }

        int count = 0;
        for (String word : words) {
            int[] letters = map.clone();
            if (helper(word, letters)) {
                count += word.length();
            }
        }
        return count;
    }

    private boolean helper(String word, int[] letters) {
        for (char c : word.toCharArray()) {
            if (letters[c - 'a'] > 0) {
                letters[c - 'a']--;
            } else {
                return false;
            }
        }
        // System.out.println(word);
        return true;
    }
}

标签:1160,Words,int,word,chars,Formed,单词,words,letters
From: https://www.cnblogs.com/cnoodle/p/17872570.html

相关文章

  • aspose-words
    aspose注册static{try(InputStreamis=AsposeUtil.class.getResourceAsStream("/license.xml")){LicenseaposeLic=newLicense();aposeLic.setLicense(is);}catch(Exceptione){e.printStackTrace();}}li......
  • com.aspose.words word 转pdf问题
    在讲word转pdf的时候推荐使用以下代码publicstaticvoidmain(String[]args)throwsException{//加载要转换的Word文档。Documentdoc=newDocument("C:\\Temp\\input.doc");//要保存输出的PDF文件的位置。StringoutputFilenam......
  • Aspose.Words使用word模板中的书签/域插入信息并导出
    首先我大概叙述一下我对这个东西的理解毕竟我也只是记录一下,确保下次自己在看的时候可以看懂,所以写的比较详细且傻瓜首先这个word文档不是凭空生成的,是你事先就把文档创建好的,里边的内容,格式都是实现创建好的只留下一些需要插入数据的地方,当然这些需要插入数据的地方也不是空着的......
  • [Java]format string is malformed java
    formatstringismalformedjava最近在做代码审查,发现很多在使用String.format的时候遇到了IDEA报的Formatstring'xxx'ismalformed警告。顾名思义,错误是标识字符串格式不正确,也就是说由于使用了格式不正确的字符串格式化指令导致的。这次发现的错误的使用如下:String.......
  • Aspose.Words解除word转pdf的页数限制
    参考文章Aspose.Words破解版用法示例Aspose.Words中文文档本篇文章可以实现解除word转pdf的页数限制,并去除水印。引入dll根据Aspose.Words破解版用法示例这篇文章中提供的dll以及证书,进行注册。右键项目添加项目引用,点击浏览选择15.7的dll进行添加在程序启动时注册......
  • 《AN IMAGE IS WORTH 16X16 WORDS: TRANSFORMERS FOR IMAGE RECOGNITION AT SCALE》阅
    论文标题《ANIMAGEISWORTH16X16WORDS:TRANSFORMERSFORIMAGERECOGNITIONATSCALE》谷歌论文起名越来越写意了,“一幅图像值16X16个单词”是什么玩意儿。ATSCALE:说明适合大规模的图片识别,也许小规模的不好使作者来自GoogleResearch的Brain团队,经典的同等贡献......
  • Aspose.Words for Java21.11去除水印和数量限制
    前言工欲善其事,必先利其器目前用到了word转pdf,然后开始研究大佬的文章,用于个人学习,首先感谢!源码分析1.下载Aspose.WordsforJava21.11官方jar包2.开始分析调用授权方法InputStreamis=newFileInputStream("..license.xml");Licenselicense=newLicense();license.setLi......
  • Aspose.Words 自动编号段落提取编号内容
    https://blog.csdn.net/u010187918/article/details/127729231如果提取不成功请确定该段落是否确实具有自动编号,如果确定,那么请在Aspose.Words.Document类对象doc使用doc.UpdateListLabels()接口后再看是否提取成功。  ///<summary>///获取List编号内容///</summary>/......
  • 531_平台屏蔽太敏感?不如试试WordsAway
    这是一篇原发布于2020-06-1913:30:00得益小站的文章,备份在此处。前言本文提到的工具仅用于帮助发送正常的内容,只能避开机器检测,若有人举报,人工审核后可能遭至更严重的处罚!发布言论在公共平台时请注意自己的一言一行!虽然这是个好东西但是切勿滥用,不然到时候某一天算法可以识别......
  • Compress Words
    CompressWords本人蒟蒻,请看更详细的题解CF1200ECompressWords题解重点是利用KMP计算最长前后缀,注意几个点:长度、越界。点击查看代码#include<bits/stdc++.h>usingnamespacestd;constintN=1e6+10;intne[N];voidkmp(stringss){ intn=ss.length(); ss......