项目作者: eMahtab

项目描述 :
Verifying an alien dictionary
高级语言:
项目地址: git://github.com/eMahtab/verifying-an-alien-dictionary.git
创建时间: 2020-05-10T10:45:11Z
项目社区:https://github.com/eMahtab/verifying-an-alien-dictionary

开源协议:

下载


Verifying an alien dictionary

https://leetcode.com/problems/verifying-an-alien-dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a 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 true if and only if the given words are sorted lexicographicaly in this alien language.

  1. Example 1:
  2. Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
  3. Output: true
  4. Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.
  5. Example 2:
  6. Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
  7. Output: false
  8. Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.
  9. Example 3:
  10. Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
  11. Output: false
  12. Explanation: The first three characters "app" match, and the second string is shorter (in size.)
  13. According to lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as
  14. the blank character which is less than any other character (More info).

Constraints:

  1. 1 <= words.length <= 100
  2. 1 <= words[i].length <= 20
  3. order.length == 26
  4. All characters in words[i] and order are English lowercase letters.

Implementation 1 : Prefix check in beginning

  1. class Solution {
  2. public boolean isAlienSorted(String[] words, String order) {
  3. int[] ordering = new int[26];
  4. for(int i = 0; i < order.length(); i++) {
  5. ordering[order.charAt(i) - 'a'] = i;
  6. }
  7. for(int i = 0; i < words.length - 1; i++) {
  8. String first = words[i];
  9. String second = words[i+1];
  10. // Check If second word is a prefix of first word, If thats the case its not a valid alien dictionary
  11. // eg. ["apple", "app"]
  12. if(first.startsWith(second) && first.length() > second.length())
  13. return false;
  14. int minLength = Math.min(first.length(), second.length());
  15. for(int index = 0; index < minLength; index++) {
  16. // Find the first different character
  17. if(first.charAt(index) != second.charAt(index)) {
  18. if(ordering[first.charAt(index) - 'a'] > ordering[second.charAt(index) - 'a'])
  19. return false;
  20. break;
  21. }
  22. }
  23. }
  24. return true;
  25. }
  26. }

Implementation 2 : Prefix check at last

  1. class Solution {
  2. public boolean isAlienSorted(String[] words, String order) {
  3. if(words == null || words.length == 0)
  4. return true;
  5. int[] chars = new int[26];
  6. for(int i = 0; i < order.length(); i++) {
  7. chars[order.charAt(i) - 'a'] = i;
  8. }
  9. for(int i = 0; i < words.length - 1; i++) {
  10. String first = words[i];
  11. String second = words[i+1];
  12. int min = Math.min(first.length(), second.length());
  13. for(int k = 0; k < min; k++) {
  14. if(first.charAt(k) != second.charAt(k)) {
  15. if(chars[first.charAt(k)-'a'] > chars[second.charAt(k) -'a'] )
  16. return false;
  17. else
  18. break;
  19. }
  20. }
  21. if(second.length() < first.length() && first.startsWith(second))
  22. return false;
  23. }
  24. return true;
  25. }
  26. }

Important :

  1. Prefix check is required to handle e.g. [“apple”, “app”] these type of inputs.
  2. Don’t compare after the first different character, so break; is must
  3. Don’t forget array’s have length property while strings have length() method

References :

https://leetcode.com/articles/verifying-an-alien-dictionary