diff --git a/c/953-Verifying-An-Alien-Dictionary.c b/c/953-Verifying-An-Alien-Dictionary.c new file mode 100644 index 000000000..dfa59f725 --- /dev/null +++ b/c/953-Verifying-An-Alien-Dictionary.c @@ -0,0 +1,34 @@ +/* +Given a sequence of words written in the alien language, and the +order of the alphabet, return true if and only if the given words +are sorted lexicographically in this alien language. + +Space: O(1) +Time: O(n*m) +(Where n is the number of words and m the average length of the elements in words) +*/ + + +bool isAlienSorted(char ** words, int wordsSize, char * order){ + int alphabet[26] = {0}; + for (int i=0; i<26; i++) + alphabet[order[i] - 'a'] = i; + for (int i=1; iv_i){ + return false; + } + break; + } + j++; + } + if (words[i-1][j]!='\0' && words[i][j] == '\0'){ // If words[i] is shorter than words[i-1] + return false; + } + } + return true; +}