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. 洛谷P3205 合唱队

    题目 区间dp.但是跟平常的区间dp不同的是,这个题仅仅只是运用了区间dp的通过小区间的信息更新大区间的信息,而没有运用枚举断点的区间dp一般思路. 这个题我们首先发现每个人在插入的时候一定插入到队伍 ...

  2. USACO 重排干草&&BZOJ1045

    USACO 重排干草&&BZOJ1045 Description 约翰订购了很多干草,他在农场里标记了 N 个位置.这些位置近似地构成一个圆环.他原打算 让送货司机在 i 号位卸下 B ...

  3. JavaScript对象及面向对象

    1.创建对象(1)自定义对象   语法:var 对象名称=new Object();(2)内置对象   String(字符串)对象.   Date(对象)对象   Array(数组)对象   Boll ...

  4. java生成HMACSHA256的方法

    data要加密的数据,key密钥 public static String HMACSHA256(String data, String key) throws Exception { Mac sha ...

  5. NIO 选择器 Selector

    选择器提供选择执行已经就绪的任务的能力,这使得多元 I/O 成为可能.就像在第一章中描述的那样,就绪选择和多元执行使得单线程能够有效率地同时管理多个 I/O 通道(Channels).C/C++代码的 ...

  6. 第10组 Beta冲刺(5/5)

    链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 将数据分析以可视化形式展示出来 新增数据分析展示等功能API 服务器后端部署, ...

  7. EF join

    两张表: var query = db.Categories // 第一张表 .Join(db.CategoryMaps, // 第二张表 c => c.CategoryId, // 主键 cm ...

  8. win7下查看进程端口

    一. 查看所有进程占用的端口 在开始-运行-cmd,输入:netstat –ano 可以查看所有进程 二.查看占用指定端口的程序 当你在用tomcat发布程序时,经常会遇到端口被占用的情况,我们想知道 ...

  9. AIX 系统参数配置

    AIX 系统参数配置 原创 Linux操作系统 作者:fanhongjie 时间:2008-05-08 22:46:37 540 0 AIX内核属于动态内核,核心参数基本上可以自动调整,因此当系统安装 ...

  10. ThinkPHP5 基础知识入门 [入门必先了解]

    一.目录结构 下载最新版框架后,解压缩到web目录下面,可以看到初始的目录结构如下: project 应用部署目录 ├─application 应用目录(可设置) │ ├─common 公共模块目录( ...