LeetCode.1002-寻找共有字符(Find Common Characters)

这是悦乐书的第375次更新,第402篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002)。给定仅由小写字母组成的字符串A,返回列表中所有字符串都有显示的字符的列表(包括重复字符)。例如,如果一个字符在所有字符串中出现3次但不是4次,则需要在最终答案中包含该字符三次。

你可以按任何顺序返回答案。例如:

输入:["bella","label","roller"]
输出:["e","l","l"]

输入:["cool","lock","cook"]
输出:["c","o"]

注意

  • 1 <= A.length <= 100
  • 1 <= A[i].length <= 100
  • A[i][j]是一个小写字母。

02 解题

题目的意思是找A中所有字符串共有的字符,即所有字符串的字符交集。比如示例中第一个字符串数组["bella","label","roller"]"bella""label"的字符交集是{‘a‘,‘b‘,‘e‘,‘l‘,‘l‘}"label""roller"的字符交集是{‘e‘,‘l‘,‘l‘},而{‘a‘,‘b‘,‘e‘,‘l‘,‘l‘}{‘e‘,‘l‘,‘l‘}的交集是{‘e‘,‘l‘,‘l‘},所以最后的结果是["e","l","l"]

思路:使用一个26个长度的int数组count,初始值设为整型最大值,循环处理A中的字符串,借助26个长度的int数组tem,将每个字符串中的字符出现次数记数,然后比较counttem中对应位的元素值大小(字符出现次数),取较小(求交集)的一个重新赋值给count中的对应位,最后将count数组中大于0的数转成字符串添加到List中去。

public List<String> commonChars(String[] A) {
    List<String> result = new ArrayList<String>();
    int[] count = new int[26];
    for (int i=0; i<26; i++) {
        count[i] = Integer.MAX_VALUE;
    }
    for (String str : A) {
        int[] tem = new int[26];
        for (int j=0; j<str.length(); j++) {
            tem[str.charAt(j)-'a']++;
        }
        for (int k=0; k<26; k++) {
            count[k] = Math.min(count[k], tem[k]);
        }
    }
    for (int i=0; i<26; i++) {
        while (count[i]-- > 0) {
            result.add((char)(i+'a')+"");
        }
    }
    return result;
}

03 小结

算法专题目前已连续日更超过七个月,算法题文章242+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

原文地址:https://www.cnblogs.com/xiaochuan94/p/11142210.html

时间: 2024-07-30 17:27:56

LeetCode.1002-寻找共有字符(Find Common Characters)的相关文章

leetcode 1002. 查找常用字符(Find Common Characters)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表.例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次. 你可以按任意顺序返回答案. 示例 1: 输入:["bella","label","roller"] 输出:["e","l","l&

【leetcode刷题笔记】Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. 题解:以strs[0]为模板,每次挨个查看是否所有的串里面是否第i位上都和strs[0]一样,如果都一样,把i位置上的字符放到answer里面,i++,继续循环,否则返回当前的answer. 代码如下: 1 public class Solution { 2 public String longestCommonPrefix

*Common characters

Write a program that gives count of common characters presented in an array of strings..(or array of character arrays) For eg.. for the following input strings.. aghkafgklt dfghako qwemnaarkf The output should be 3. because the characters a, f and k

Swift学习笔记-字符串和字符(Strings and Characters)-Unicode

Unicode Unicode 是一个国际标准,用于文本的编码和表示. 它使您可以用标准格式表示来自任意语言几乎所有的字符,并能够对文本文件或网页这样的外部资源中的字符进行读写操作. Swift 的String和Character类型是完全兼容 Unicode 标准的. Unicode 标量(Unicode Scalars) Swift 的String类型是基于 Unicode 标量 建立的. Unicode 标量是对应字符或者修饰符的唯一的21位数字,例如U+0061表示小写的拉丁字母(LAT

LeetCode:寻找重复数【287】

LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: 输入: [1,3,4,2,2] 输出: 2 示例 2: 输入: [3,1,3,4,2] 输出: 3 说明: 不能更改原数组(假设数组是只读的). 只能使用额外的 O(1) 的空间. 时间复杂度小于 O(n2) . 数组中只有一个重复的数字,但它可能不止重复出现一次.

【Golang语言】LeetCode 1002. Find Common Characters

Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates). For example, if a character occurs 3 times in all strings but not 4 times, you need to i

Leetcode 1002. Find Common Characters

python可重集合操作 class Solution(object): def commonChars(self, A): """ :type A: List[str] :rtype: List[str] """ if not A: return [] from collections import Counter ans=Counter(A[0]) for str in A: ans&=Counter(str) ans=list(an

【Leetcode】【Longest Substring Without Repeating Characters】【无重复字符的最长子串】【C++】

题目:给定一字符串,求其无重复字符的最长子串长度. 思路:for循环一次,时间复杂度为O(N).字符的ascii值为32~126.start表示当前无重复字符子串的初始位置,初始值为0:可定义一个位置数组pos[128]表示for循环索引到当前位置时相应的字符对应的位置.若当前字符s[i](其ascii值为cur_pos),若pos[cur_pos]>=start,说明在start之后已有该字符s[i],则以start开始的子串第一次遇到重复字符,打住.判断当前长度是否大于max_len,若大于

leetcode 3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)

目录 题目描述: 示例 1: 示例 2: 示例 3: 解法: 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3. 示例 2: 输入: "bbbbb" 输出: 1 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1. 示例 3: 输入: "pwwkew"