<style> html, body, .ui-content { background: #222222; color: #00BFFF; } /* 設定 code 模板 */ .markdown-body code, .markdown-body tt { background-color: #ffffff36; } .markdown-body .highlight pre, .markdown-body pre { color: #ddd; background-color: #00000036; } .hljs-tag { color: #ddd; } .token.operator { background-color: transparent; } /* 設定連結 */ a, .open-files-container li.selected a { color: #89FFF8; } a:hover, .open-files-container li.selected a:hover { color: #89FFF890; } </style> ###### tags: `Leetcode` # 953. Verifying an Alien Dictionary ###### Link : https://leetcode.com/problems/verifying-an-alien-dictionary/description/ ## 題目 判斷所有字串有沒有按照題目給的字典序排序。 ## 程式碼 ```cpp= class Solution { public: bool isAlienSorted(vector<string>& words, string order) { vector<int> Order(26); //列出小寫英文字元的所有大小 for(int i = 0;i < order.size();i++) Order[order[i] - 'a'] = i; for(int i = 0;i < words.size() - 1;i++) //如果前者比後者大,回傳false if(!isSorted(Order, words[i], words[i + 1])) return false; //所有都符合規則,回傳true return true; } bool isSorted(vector<int> &Order, string &str1, string &str2){ for(int i = 0;i < str1.size();i++){ //大於回傳False;小於回傳true;等於繼續跑迴圈 if(str2[i] == '\0' || Order[str1[i]-'a'] > Order[str2[i]-'a']) return false; else if(Order[str1[i]-'a'] < Order[str2[i]-'a']) return true; } return true; } }; ``` ## Date ### 2023/2/2