mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-21 21:11:57 +03:00
add leetcode Verifying an Alien Dictionary (#1205)
* add leetcode Verifying an Alien Dictionary * updating DIRECTORY.md * Update 953.c add blank line at the end --------- Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com> Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
parent
55f73501ea
commit
4830210f69
@ -110,6 +110,7 @@
|
||||
| 917 | [Reverse Only Letters](https://leetcode.com/problems/reverse-only-letters) | [C](./src/917.c) | Easy |
|
||||
| 931 | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum) | [C](./src/931.c) | Medium |
|
||||
| 938 | [Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst) | [C](./src/938.c) | Easy |
|
||||
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/problems/verifying-an-alien-dictionary) | [C](./src/953.c) | Easy |
|
||||
| 965 | [Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree) | [C](./src/965.c) | Easy |
|
||||
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array) | [C](./src/977.c) | Easy |
|
||||
| 979 | [Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree) | [C](./src/979.c) | Medium |
|
||||
|
40
leetcode/src/953.c
Normal file
40
leetcode/src/953.c
Normal file
@ -0,0 +1,40 @@
|
||||
#define min(x, y) (((x) < (y)) ? (x) : (y))
|
||||
|
||||
bool isWordLess(char* word1, char* word2, int* charOrder){
|
||||
int word1Length = strlen(word1);
|
||||
int word2Length = strlen(word2);
|
||||
|
||||
for(int i = 0; i < min(word1Length, word2Length); i++) {
|
||||
int charWordsDiff = (charOrder[word1[i] - 'a'] - charOrder[word2[i] - 'a']);
|
||||
|
||||
if (charWordsDiff < 0){
|
||||
return true;
|
||||
}
|
||||
|
||||
if (charWordsDiff > 0){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return word1Length <= word2Length;
|
||||
}
|
||||
|
||||
// Keep array-hashtable of order letters.
|
||||
// Runtime: O(n)
|
||||
// Space: O(1)
|
||||
bool isAlienSorted(char ** words, int wordsSize, char * order){
|
||||
const int lowerCaseLettersNumber = 26;
|
||||
int charorder[lowerCaseLettersNumber];
|
||||
|
||||
for(int i = 0; i < lowerCaseLettersNumber; i++) {
|
||||
charorder[order[i] - 'a'] = i;
|
||||
}
|
||||
|
||||
for(int i = 0; i < wordsSize - 1; i++) {
|
||||
if (!isWordLess(words[i], words[i + 1], charorder)){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Loading…
Reference in New Issue
Block a user