Created
November 22, 2023 09:35
-
-
Save juanfal/1b181c7c25d6af7288678d09a6c5ccae to your computer and use it in GitHub Desktop.
isograms
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
// t11e10.isograms.cpp | |
// juanfc 2023-11-22 | |
// https://gist.github.com/juanfal/1b181c7c25d6af7288678d09a6c5ccae | |
#include <iostream> | |
#include <array> | |
using namespace std; | |
typedef array<int,256> TCharFreq; | |
void test(string s); | |
int main() | |
{ | |
test("Dermatoglyphics" ); | |
test("aba" ); | |
test("moOse" ); | |
return 0; | |
} | |
bool isIsogram(string s); | |
void test(string s) | |
{ | |
if (isIsogram(s)) | |
cout << s << " IS isogram" << endl; | |
else | |
cout << s << " is NOT isogram" << endl; | |
} | |
TCharFreq getFreqs(string s); | |
bool isIsogram(string s) | |
{ | |
TCharFreq f = getFreqs(s); | |
int i = 0; | |
while (i < 256 and f[i] < 2) | |
++i; | |
return i == 256; | |
} | |
char toLow(char c); | |
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[toLow(s[i])]; | |
return r; | |
} | |
char toLow(char c) | |
{ | |
char r = c; | |
if ('A' <= c and c <= 'Z') | |
r = c + 'a'-'A'; | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment