Refactor c/isogram to check for null char directly

This gets us from 2n to n.
This commit is contained in:
dwrz
2019-02-26 13:22:57 +00:00
parent fe464969b8
commit 995f4b4856
+1 -3
View File
@@ -17,14 +17,12 @@ bool is_symbol_char(char c) {
bool is_isogram(const char phrase[]) { bool is_isogram(const char phrase[]) {
if (phrase == NULL) { return false; } if (phrase == NULL) { return false; }
if (length(phrase) == 0) { return true; }
// There are 26 lowercased ASCII alpha chars (97-122). // There are 26 lowercased ASCII alpha chars (97-122).
// We can use 26 indexes to keep track of each one. // We can use 26 indexes to keep track of each one.
char char_map[26] = { 0 }; char char_map[26] = { 0 };
for (int i = 0; i < length(phrase); i++) { for (int i = 0; phrase[i] != '\0'; i++) {
if (is_symbol_char(phrase[i])) { continue; } // Ignore non-alpha chars. if (is_symbol_char(phrase[i])) { continue; } // Ignore non-alpha chars.
if (char_map[lowercase(phrase[i]) - 97] != 0) { return false; } if (char_map[lowercase(phrase[i]) - 97] != 0) { return false; }
// Increment the count for this char.
char_map[lowercase(phrase[i]) - 97]++; char_map[lowercase(phrase[i]) - 97]++;
} }
return true; return true;