commit 995f4b4856a459248b5c7707efddff7049c52521
parent fe464969b899cb033a5899346ccfd1a3ba87d0ed
Author: dwrz <dwrz@dwrz.net>
Date: Tue, 26 Feb 2019 13:22:57 +0000
Refactor c/isogram to check for null char directly
This gets us from 2n to n.
Diffstat:
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/c/isogram/src/isogram.c b/c/isogram/src/isogram.c
@@ -17,14 +17,12 @@ bool is_symbol_char(char c) {
bool is_isogram(const char phrase[]) {
if (phrase == NULL) { return false; }
- if (length(phrase) == 0) { return true; }
// There are 26 lowercased ASCII alpha chars (97-122).
// We can use 26 indexes to keep track of each one.
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 (char_map[lowercase(phrase[i]) - 97] != 0) { return false; }
- // Increment the count for this char.
char_map[lowercase(phrase[i]) - 97]++;
}
return true;