From 995f4b4856a459248b5c7707efddff7049c52521 Mon Sep 17 00:00:00 2001 From: dwrz Date: Tue, 26 Feb 2019 13:22:57 +0000 Subject: [PATCH] Refactor c/isogram to check for null char directly This gets us from 2n to n. --- c/isogram/src/isogram.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/c/isogram/src/isogram.c b/c/isogram/src/isogram.c index eb21dd1..30093a8 100644 --- 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;