Last active
November 22, 2023 19:39
-
-
Save juanfal/c9714ca8843727fa6bf179ae6923ca1a to your computer and use it in GitHub Desktop.
counting char freqs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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