Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1348 from julienChemillier/patch-35
Browse files Browse the repository at this point in the history
Add 953 in c language
  • Loading branch information
Ahmad-A0 authored Nov 2, 2022
2 parents 638efa9 + d7f2cb9 commit 73f48a4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions c/953-Verifying-An-Alien-Dictionary.c
Original file line number Diff line number Diff line change
@@ -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; i<wordsSize; i++) { // We compare words[i-1] and words[i]
int j=0;
while (words[i-1][j]!='\0' && words[i][j]!='\0') { // Iterates through all letters of words
int v_i_1 = alphabet[words[i-1][j] - 'a'];
int v_i = alphabet[words[i][j] - 'a'];
if (v_i_1 != v_i) {
if (v_i_1>v_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;
}

0 comments on commit 73f48a4

Please sign in to comment.