일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 코딩테스트
- 머신러닝
- 온라인석사
- 위니펙
- omscs
- 방송통신대학교
- 조지아텍
- 기본
- LAA
- Deep learning
- 캐나다 영주권
- C++
- 매트랩
- 프로그래머스
- MATLAB
- EOI
- mpnp
- cpp
- 주정부이민
- machine learning
- LV1
- 선형대수
- leetcode
- 딥러닝
- zeros
- 방통대
- 개발자
- Plotting
- 컴퓨터과학과
- 알고리즘
Archives
- Today
- Total
Byte by Byte
1002. Find Common Characters.cpp 본문
🔑
1. 이전 워드에서 각 알파벳 카운트 저장(check[26])
2. 다음 워드에서 각 알파벳 카운트 저장(tmp[26])
3. tmp[26]에 0인 것 -> check[26] 도 0 대입.
4. 만약 둘다 양수 일 경우, 적은 것을 check[26]에 대입.
5. 워드 끝까지 반복.
6. check[26]에 저장된 수 만큼 character를 string으로 변환하여 res vector에 삽입.
class Solution {
public:
vector<string> commonChars(vector<string>& words) {
vector<string>res;
int check[26] = {0,};
for(int j =0; j<words[0].size(); j++){
check[words[0][j]-'a']++;
}
for(int i =1; i<words.size(); i++){
int tmp[26]={0,};
for(int j =0; j<words[i].size(); j++){
tmp[words[i][j]-'a']++;
}
for(int k = 0; k<26; k++){
if(tmp[k]==0){
check[k]=0;
}
else{
check[k] = min(check[k],tmp[k]);
}
}
}
for(int k =0; k<26; k++){
for(int i =0; i<check[k]; i++){
string x = "";
x += char('a'+k);
res.push_back(x);
}
}
return res;
}
};
'개발 로그 > 알고리즘' 카테고리의 다른 글
349. Intersection of Two Arrays.cpp (0) | 2021.11.20 |
---|---|
1636. Sort Array by Increasing Frequency.cpp (0) | 2021.11.20 |
1030. Matrix Cells in Distance Order.cpp (0) | 2021.11.19 |
821. Shortest Distance to a Character.cpp (0) | 2021.11.16 |
1356. Sort Integers by The Number of 1 Bits.cpp (0) | 2021.11.07 |