1002. 查找常用字符

给定仅有小写字母组成的字符串数组 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 List<String> commonChars(String[] A) {
        int[][] help = new int[A.length][26];
        for(int i=0;i<A.length;i++){
            put(help,A[i],i);
        }

        List<String> list = new ArrayList<>();
        for(int i=0;i<26;i++){
            int min = Integer.MAX_VALUE;
            for(int j=0;j<A.length;j++){
                min = min < help[j][i] ? min : help[j][i];
            }
            while(min-->0){
                list.add(Character.toString((char) (‘a‘ + i)));
            }
        }
        return list;
    }

    private void put(int[][]arr,String str,int index){
        for(int i=0;i<str.length();i++){
            arr[index][str.charAt(i)-‘a‘]++;
        }
    }
}

原文地址:https://www.cnblogs.com/czsy/p/10960975.html

时间: 2024-07-30 17:26:15

1002. 查找常用字符的相关文章

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

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

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

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

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

*字符串-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

html常用字符

在web开发经常会遇到如:   这样的字符.它其实是Html将一些特殊字符(Html语法字符)的一种表达方式. 下面列举几个常用字符:    空格 &   & <       < >      > "   " &qpos;   '

delphi 常用字符函数

二.去空格函数 1.LTRIM() 把字符串头部的空格去掉. 2.RTRIM() 把字符串尾部的空格去掉. 三.取子串函数 1.leftstr() LEFTstr (<str>, <integer_expression>) 返回str 左起 integer_expression 个字符. 2.RIGHTstr() RIGHTstr (<str>, <integer_expression>) 返回str 右起 integer_expression 个字符. 3

在字符串中查找指定字符

输入一个字符串S,再输入一个字符c,要求在字符串S中查找字符c.如果找不到则输出“Not found”:若找到则输出字符串S中从c开始的所有字符. 输入格式: 输入在第1行中给出一个不超过80个字符长度的.以回车结束的非空字符串:在第2行中给出一个字符. 输出格式: 在一行中按照题目要求输出结果. 输入样例1: It is a black box b 输出样例1: black box 输入样例2: It is a black box B 输出样例2: Not found #include<std