当前位置:网站首页>Code random notes_ Hash_ 1002 find common characters
Code random notes_ Hash_ 1002 find common characters
2022-07-28 01:04:00 【Erik_ Won】
Code random notes _ Hashtable
Code Capriccio record two brush notes
LC1002. Find common characters
subject
Here's an array of strings words , Please find out all in words A common character that appears in each string of ( Include repeating characters ), And return as an array . You can press In any order Return to the answer .
Example 1:
Input :words = [“bella”,“label”,“roller”]
Output :[“e”,“l”,“l”]
Example 2:
Input :words = [“cool”,“lock”,“cook”]
Output :[“c”,“o”]
Thought analysis
- Topic meaning analysis : from 26 Of the lowercase characters , Have a character (a) In all strings (aapply/aapple/aaugment) If it appears in , It outputs , Repeat it , The output [‘a’,‘a’],b,c,d,…z Empathy
- Ideas : Hash array , Record the hash value of each string , Minimum value
- Deductive analysis : Hash table statistics words in 26 Frequency of characters , Then take the minimum value of each string , Then convert to characters (String) Output

a b e l o r z
bella: [1 1 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
label: [1 1 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
roller: [0 0 0 0 1 0 0 0 0 0 0 2 0 0 1 0 0 2 0 0 0 0 0 0 0 0 ]
Math.min: [0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
Get the output ["e","l","l"]
Code implementation
Complete code implementation
public List<String> commonChars(String[] words) {
List<String> res = new ArrayList<>();
if(words.length == 0) return res;
// Scan first String, initialization hash
int[] record = new int[26]; // Record No. 1 individual String Of Hash
for (int i = 0; i < words[0].length();i++){
record[words[0].charAt(i) - 'a']++;
}
// After scanning String
for (int i = 1; i < words.length;i++){
int[] temp = new int[26]; // Record No. i individual String Of Hash
for (int j = 0; j < words[i].length();j++){
temp[words[i].charAt(j) - 'a']++;
}
// contrast The first i individual String And the i-1 individual String Of Hash
for(int k = 0; k < record.length;k++){
record[k] = Math.min(record[k],temp[k]); take Hash minimum value
}
}
// Convert to String
for (int i = 0;i < record.length;i++) {
while (record[i] > 0){
// Because there is repetition , So we need to while
char c = (char)(i+'a');
res.add(String.valueOf(c));
record[i]--;
}
}
return res;
}
边栏推荐
- 【STM32】看门狗模块
- From the second floor to the third floor
- 推荐系统-模型:wide&deep 模型
- [proteus simulation] 51 single chip microcomputer washing machine simulation control program
- 592. Fraction addition and subtraction: introduction to expression calculation
- startUMl
- 计算属性的基本使用
- Leetcode - find the median of two positively ordered arrays
- DC motor winding parameters
- Canvas analog input box input
猜你喜欢
![Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]](/img/6c/d4a45981a7fc87f6a82a91017f8ce8.png)
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]

C语言程序设计 | offsetof宏的讲解及其模拟实现

Multithreading and multithreaded programming

【OpenCV 例程 300篇】241. 尺度不变特征变换(SIFT)

Basic operations of MySQL database (2) --- Based on data table
![[CruiseControl]Build Result JSP](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[CruiseControl]Build Result JSP

函数相关知识

iperf安装与使用

Point divide and conquer analysis

LSB steganography
随机推荐
Network device hard core technology insider firewall and security gateway (IX) virtualization artifact (II)
Recommended system model (III): fine scheduling model [LR, gbdt, wide & deep, DCN, DIN, Dien, MMOE, ple]
Impact of privilege changes on existing connections
Invest 8billion! Nanjing Huatian sealed test phase I project is about to be put into production!
Examples of application of JMeter in performance testing
代码随想录笔记_哈希_1002查找共用字符
Jerry Zhi doesn't play hidden audio files [article]
The Canadian court found Meng Wanzhou guilty of "dual criminality", and the extradition procedure will continue!
估值360亿美元!即将进行首次载人发射的SpaceX筹资3.46亿美元
c# 反射之Type使用
怎么清晰地理解、表达 IaaS 、 PaaS 、 SaaS ?
Interesting Huffman tree
Recommended system - fine tuning model: xdeepfm
canvas模拟输入框输入
592. Fraction addition and subtraction: introduction to expression calculation
C语言程序设计 | 单身狗题目讲解
Wavelet transform learning notes
Jmeter在性能测试中的应用实践样例
Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
One year anniversary of creation, Chongba young Lang