package y2019.Algorithm.array;

import java.util.*;

/**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: CommonChars
* @Author: xiaof
* @Description: 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 include that character three times
* in the final answer.
* You may return the answer in any order.
*
* Input: ["bella","label","roller"]
* Output: ["e","l","l"]
*
* @Date: 2019/7/4 11:07
* @Version: 1.0
*/
public class CommonChars { public List<String> solution(String[] A) {
List<String> result = new ArrayList<>();
int[] nums = new int[26]; //英文一共26个字符,并且是小写的
Arrays.fill(nums, Integer.MAX_VALUE);
//遍历所有的字符,根据小标放入集合中
for(String aTemp : A) {
//依次遍历所有字符
char tempC[] = aTemp.toCharArray();
int[] cnt = new int[26];
//第一次统计每个单词中出现的次数
for(int j = 0; j < tempC.length; ++j) {
cnt[tempC[j] - 'a']++;
}
//第二次我们过滤掉,每二个单词中出现的最小次数,比如第一个单词出现10次,但是第二个单词出现1次,那么都出现的次数也就是1次
for(int i = 0; i < nums.length; ++i) {
nums[i] = Math.min(nums[i], cnt[i]);
}
} //最后统计结果
for(int i = 0; i < nums.length; ++i) {
for(int j = 0; j < nums[i]; ++j) {
//如果出现多处,那么放入多处
result.add("" + (char) ('a' + i));
}
} //如果每个字符中都出现过,那么必须是3的倍数次
return result;
} public List<String> commonChars(String[] A) {
List<String> ans = new ArrayList<>();
int[] count = new int[26];
Arrays.fill(count, Integer.MAX_VALUE);
for (String str : A) {
int[] cnt = new int[26];
for (int i = 0; i < str.length(); ++i) { ++cnt[str.charAt(i) - 'a']; } // count each char's frequency in string str.
for (int i = 0; i < 26; ++i) { count[i] = Math.min(cnt[i], count[i]); } // update minimum frequency.
}
for (char c = 'a'; c <= 'z'; ++c) {
while (count[c - 'a']-- > 0) { ans.add("" + c); }
}
return ans;
} public static void main(String args[]) {
String as[] = {"bella","label","roller"};
CommonChars fuc = new CommonChars();
System.out.println(fuc.solution(as));
}
}

【LEETCODE】43、1002. Find Common Characters的更多相关文章

  1. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  2. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  5. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  6. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

  7. 【LeetCode】2、Add Two Numbers

    题目等级:Medium 题目描述:   You are given two non-empty linked lists representing two non-negative integers. ...

  8. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

  9. 【LEETCODE】73、根据身高重建队列 第406题

    说实话,这道题我没想出来,但是看解题报告题解比较让人觉得眼前一亮,这里记录下来 package y2019.Algorithm.greedy.medium; import java.util.Arra ...

随机推荐

  1. Collecting metrics with the PostgreSQL and TimescaleDB output plugin for Telegraf

    转自:https://docs.timescale.com/v1.3/tutorials/telegraf-output-plugin 文章演示了如何使用pg output 插件 以及Telegraf ...

  2. CSPS_111

    这场是众神的AKsh♂ow 而我T2 long long没开够没有AK 如果这是CS... T1 迭代就可以 T2 设x不断除2直到x为奇数得到的奇数为y 则y相同的所有x明显分成了两个互斥的部分 对 ...

  3. 【洛谷P5019】铺设道路

    题目链接 众所周知,这道题和积木大赛是同一道题 题意就是给出一段自然数序列,每次操作\((L,R)\)把区间\([L,R]\)的数全部减一,不允许出现负数,问把序列变为零的最小操作次数 贪心做法 样例 ...

  4. 初识 Python 作业及默写

    1.简述变量量命名规范 2.name = input(“>>>”) name变量是什么数据类型? 3.if条件语句的基本结构? 4.用print打印出下面内容: 文能提笔安天下, 武 ...

  5. [golang]Golang实现高并发的调度模型---MPG模式

    Golang实现高并发的调度模型---MPG模式 传统的并发形式:多线程共享内存,这也是Java.C#或者C++等语言中的多线程开发的常规方法,其实golang语言也支持这种传统模式,另外一种是Go语 ...

  6. 算法练习题---罗马数字转int

    连接:https://leetcode-cn.com/problems/roman-to-integer/submissions/ 题目: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 ...

  7. Django入门——《Python编程从入门到实践》

    Django是一个Web框架--一套用于帮助开发交互式网站的工具.Django能够响应网页请求,还能让你更轻松地读写数据库.管理用户等. 1.建立项目 开始编写一个名为"学习笔记" ...

  8. 【数值分析】Python实现Lagrange插值

    一直想把这几个插值公式用代码实现一下,今天闲着没事,尝试尝试. 先从最简单的拉格朗日插值开始!关于拉格朗日插值公式的基础知识就不赘述,百度上一搜一大堆. 基本思路是首先从文件读入给出的样本点,根据输入 ...

  9. 反向传播BP算法

    前向传播模型 一般我们使用的公式是: \[ a=\frac{1}{1+\exp \left(-\left(w^{T} x+b\right)\right)} = \frac{1}{1+\exp \lef ...

  10. vs2015编译OBS-Studio21.1.12

    原文地址:http://www.freesion.com/article/37445100/ 参考:https://blog.csdn.net/su_vast/article/details/7498 ...