[AlgoExpert] Minimum Characters for Words

Minimum Characters for Words

  • Time : O(nlogn)
  • Space : O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <vector>
using namespace std;

vector<char> minimumCharactersForWords(vector<string> words) {
unordered_map<char,int> mp;
for(auto& w : words) {
unordered_map<int, int> counter;
for(auto& ch : w) counter[ch]++;
for(auto& [ch, count] : counter)
mp[ch] = max(mp[ch], count);
}
vector<char> res;
for(auto& [ch, count] : mp) {
for(int i = 0; i < count; i++)
res.push_back(ch);
}
sort(begin(res), end(res));
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/12/PS/AlgoExpert/minimum-characters-for-words/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.