GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/953_Verifying_an_Alien_Dictionary.py at master · qiyuangong/leetcode Expand file tree/ Copy path953_Verifying_an_Alien_Dictionary.pyMore file actions 32 lines (29 loc) · 1008 Bytes / Copy path953_Verifying_an_Alien_Dictionary.pyFile metadata and controls32 lines (29 loc) · 1008 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 class Solution(object): def isAlienSorted(self, words, order): """ :type words: List[str] :type order: str :rtype: bool """ order_map = {} for i, v in enumerate(order): order_map[v] = i def cmp_alien(x, y): ls = min(len(x), len(y)) index = 0 while index < ls: if x[index] != y[index]: return order_map[x[index]] - order_map[y[index]] index += 1 return len(x) - len(y) pos = 0 while pos + 1 < len(words): if cmp_alien(words[pos], words[pos + 1]) > 0: return False pos += 1 return True if __name__ == '__main__': s = Solution() print s.isAlienSorted(["hello","leetcode"], "hlabcdefgijkmnopqrstuvwxyz") print s.isAlienSorted(["word","world","row"], "worldabcefghijkmnpqstuvxyz") print s.isAlienSorted(["apple","app"], "abcdefghijklmnopqrstuvwxyz") |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |