Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 22, 2023 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/c9714ca8843727fa6bf179ae6923ca1a to your computer and use it in GitHub Desktop.
Save juanfal/c9714ca8843727fa6bf179ae6923ca1a to your computer and use it in GitHub Desktop.
counting char freqs
// t11e16.stringdiff.cpp
// juanfc 2023-11-22
// https://gist.github.com/c9714ca8843727fa6bf179ae6923ca1a
#include <iostream>
#include <array>
using namespace std;
string diff(string a, string b);
int main()
{
cout << diff("telephone", "phonograph") << endl;
cout << diff("holograph", "dog") << endl;
return 0;
}
typedef array<int,256> TCharFreq;
TCharFreq getFreqs(string s);
string diff(string a, string b)
{
string res;
TCharFreq freqs_b = getFreqs(b);
for ( int i = 0; i < a.size(); ++i )
if (freqs_b[a[i]])
--freqs_b[a[i]];
else
res += a[i];
return res;
}
TCharFreq getFreqs(string s)
{
TCharFreq r;
for (int i = 0; i < 256; ++i)
r[i] = 0;
for (int i = 0; i < s.length(); ++i)
++r[s[i]];
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment