diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /verify_alien_dict.c | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'verify_alien_dict.c')
-rw-r--r-- | verify_alien_dict.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/verify_alien_dict.c b/verify_alien_dict.c new file mode 100644 index 0000000..c0fe1f0 --- /dev/null +++ b/verify_alien_dict.c @@ -0,0 +1,34 @@ +// 953. Verifying an Alien Dictionary +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +/* + * in an alien language, they also use english lowercase letters but in + * different 'order'. the 'order' of the alphabet is some permutation of + * lowercase letters. given a sequence of 'words' written in the alien language, + * and the 'order' of the alphabet, return 1 if and only if the given 'words' + * are sorted lexicographically in this alien language. + */ + +bool cmp(char *a, char *b, int *j) { + int sizeA = strlen(a), sizeB = strlen(b); + for (int i = 0; i < sizeA && i < sizeB; i++) { + if (j[a[i] - 'a'] < j[b[i] - 'a']) + return true; + else if (j[a[i] - 'a'] > j[b[i] - 'a']) + return false; + } + return sizeA < sizeB; +} + +bool isAlienSorted(char **words, int wordsSize, char *order) { + int j[26] = {0}; + for (int i = 0; i < 26; i++) + j[order[i] - 'a'] = i; + for (int i = 0; i < wordsSize - 1; i++) + if (!cmp(words[i], words[i + 1], j)) + return false; + return true; +} |