Loading [MathJax]/jax/output/HTML-CSS/jax.js
[InterviewBit] String And Its Frequency

String And Its Frequency

  • Time : O(n)
  • Space : O(1)
c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string Solution::solve(string A) {
unordered_map<char,int> freq;
for(auto a : A) freq[a]++;
string res = "";
for(auto a : A) {
if(freq.count(a)) {
res.push_back(a);
res += to_string(freq[a]);
freq.erase(a);
}
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/14/PS/interviewbit/string-and-its-frequency/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Related Issues not found

Please contact @SongHayoung to initialize the comment