当前位置:网站首页>Force buckle 1002 Find common characters
Force buckle 1002 Find common characters
2022-07-07 11:08:00 【Exy-】
class Solution {
public:
vector<string> commonChars(vector<string>& A) {
vector<string> result;
if (A.size() == 0) return result;
int hash[26] = {0}; // It is used to count the minimum frequency of characters in all strings
for (int i = 0; i < A[0].size(); i++) { // Give... The first string hash initialization
hash[A[0][i] - 'a']++;
}
int hashOtherStr[26] = {0}; // Count the frequency of characters except the first string
for (int i = 1; i < A.size(); i++) {
memset(hashOtherStr, 0, 26 * sizeof(int));
for (int j = 0; j < A[i].size(); j++) {
hashOtherStr[A[i][j] - 'a']++;
}
// to update hash, Guarantee hash Li statistics 26 The minimum number of times a character appears in all strings
for (int k = 0; k < 26; k++) {
hash[k] = min(hash[k], hashOtherStr[k]);
}
}
// take hash Count the number of characters , Into output form
for (int i = 0; i < 26; i++) {
while (hash[i] != 0) { // Notice that this is while, Multiple duplicate characters
string s(1, i + 'a'); // char -> string
result.push_back(s);
hash[i]--;
}
}
return result;
}
};
边栏推荐
- Interprocess communication (IPC)
- VR development optimization
- 【STM32】实战3.1—用STM32与TB6600驱动器驱动42步进电机(一)
- The use of list and Its Simulation Implementation
- P1223 queuing for water /1319: [example 6.1] queuing for water
- Rolling puddle Uni_ App (VIII)
- Go-Redis 中间件
- POJ1821 Fence 题解报告
- 软考信息处理技术员有哪些备考资料与方法?
- Some online academic report websites and machine learning videos
猜你喜欢
ADB utility commands (network package, log, tuning related)
[STM32] actual combat 3.1 - drive 42 stepper motors with STM32 and tb6600 drivers (I)
CSAPP bomb lab parsing
The concept, implementation and analysis of binary search tree (BST)
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
【亲测可行】error while loading shared libraries的解决方案
单调性约束与反单调性约束的区别 monotonicity and anti-monotonicity constraint
2021 summary and 2022 outlook
Transaction rolled back because it has been marked as rollback-only解决
Idea shortcut keys
随机推荐
POJ1821 Fence 题解报告
Kitex 重试机制
mif 文件格式记录
seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)
2022年7月10日“五心公益”活动通知+报名入口(二维码)
Opencv installation and environment configuration - vs2017
The concept, implementation and analysis of binary search tree (BST)
A case of compiling QT file qmake compiling script
Those confusing concepts (3): function and class
“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题
[untitled]
【PyTorch 07】 动手学深度学习——chapter_preliminaries/ndarray 习题动手版
[untitled]
QT document
How to play video on unityui
China Southern Airlines pa3.1
What are the contents of the intermediate soft test, the software designer test, and the test outline?
单调性约束与反单调性约束的区别 monotonicity and anti-monotonicity constraint
2022.7.3DAY595
[recommendation system 01] rechub