From d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa Mon Sep 17 00:00:00 2001 From: jack <0x6A73@pm.me> Date: Fri, 14 Jun 2024 13:13:15 -0600 Subject: initial --- verify_alien_dict.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 verify_alien_dict.c (limited to 'verify_alien_dict.c') 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 +#include +#include +#include + +/* + * 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; +} -- cgit v1.2.3