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

目录

  • 题目描述:
  • 示例 1:
  • 示例 2:
  • 解法:

题目描述:

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

示例 1:

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

示例 2:

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

提示:

  1. 1 <= A.length <= 100
  2. 1 <= A[i].length <= 100
  3. A[i][j] 是小写字母

解法:

class Solution {
public:
    vector<int> parse(string s){
        vector<int> count(128, 0);
        for(char ch : s){
            count[ch]++;
        }
        return count;
    }

    void merge(vector<int> & lst1, vector<int>& lst2){
        for(char ch = 'a'; ch <= 'z'; ch++){
            lst1[ch] = min(lst1[ch], lst2[ch]);
        }
    }

    vector<string> commonChars(vector<string>& A) {
        int sz = A.size();
        vector<string> res;
        if(sz == 1){
            for(char ch : A[0]){
                res.push_back(string(1, ch));
            }
            return res;
        }
        vector<int> lst1 = parse(A[0]);
        vector<int> lst2;
        for(int i = 1; i < sz; i++){
            lst2 = parse(A[i]);
            merge(lst1, lst2);
        }
        for(int ch = 'a'; ch <= 'z'; ch++){
            // cout<<char(ch)<<": "<<lst1[ch]<<endl;
            for(int i = 0; i < lst1[ch]; i++){
                res.push_back(string(1, ch));
            }
        }
        return res;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10674442.html

时间: 2024-08-29 22:54:01

leetcode 1002. 查找常用字符(Find Common Characters)的相关文章

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

这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都有显示的字符的列表(包括重复字符).例如,如果一个字符在所有字符串中出现3次但不是4次,则需要在最终答案中包含该字符三次. 你可以按任何顺序返回答案.例如: 输入:["bella","label","roller"] 输出:["e"

1002. 查找常用字符

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

Leetcode-1002 Find Common Characters(查找常用字符)

1 #define pb push_back 2 #define maxSize 3939 3 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 4 5 const int N = 101; 6 class Solution 7 { 8 public: 9 vector<string> commonChars(vector<string>& A) 10 { 11 int hash[N][N]; 12 memset(ha

Linux 常用命令 (common commands for linux)

Linux 常用命令 (Common Commands For Linux) 1.声明,此文章仅写基于 Bash shell 常用的命令,如果遇上命令在使用过程中提示没有,可能随着更新,命令也被替换掉了,请去官方 WIKI 查找或通过 MAN 手册查看. 2.根据实际情况高效地组合各种命令选择和命令参数: 命令名称 [命令参数] [命令对象] 注意,命令名称 · 命令参数 · 命令对象之间请用空格键分隔.命令对象一般是指要处理的目标(普通文件/目录文件/用户等),命令参数可使用长格式或短格式,分

Oracle中chr()和ascii()函数(附:常用字符与ascii对照表)

Oracle中chr()和ascii()函数(附:常用字符与ascii对照表) 关键字:chr() chr()函数作用:"特殊"字符特殊处理 在PLSql中可查询相对应的字码与特殊符 chr()函数示例: select chr(38) from dual;  ascii()函数示例: select ascii('&') from dual;      比如"&"到底为什么在Oracle中成了特殊字符呢?经过查找,终于揭晓了答案:原来&这个字符

【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

*字符串-01. 在字符串中查找指定字符

1 /* 2 * Main.c 3 * D1-字符串-01. 在字符串中查找指定字符 4 * Created on: 2014年8月18日 5 * Author: Boomkeeper 6 *****部分通过****** 7 */ 8 9 #include <stdio.h> 10 11 int mysearch(char ch, const char str[], int length) { 12 13 int j, ret = -1; 14 15 for (j = 0; j < le

java语言在某个数组中查找某个字符出现的次数

package com.llh.demo; import java.util.Scanner; /** * * @author llh * */ public class Test { /* * 在某个字符数组中查找某个字符出现的次数 */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个字符:"); char a = sc.ne

MySQL常用字符函数简介

<html> <body> <h1>MySQL常用字符函数简介</h1> <table>     <tr>         <td>CONCAT(S1,S2...Sn)</td>         <td>连接S1,S2...Sn为一个字符串</td>     </tr> </table> <p style="background-color:yel