作者:
Rushia (みけねこ的鼻屎)
2023-02-02 14:19:00953. Verifying an Alien Dictionary
某個外星語的單字是由小寫字母組成但是字母的順序不同,給定一個字串陣列words[]
,和一個order表示外星語的字母順序,判斷words裡面的單字是否是按照外星語的字
母順序排列,若是則返回true,否則返回false。
Example:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: h的順序在l之前所以words[0]比words[1]小
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: d的順序在l之後,所以words[0]比words[1]大,順序和order不同。
思路:
1.用一個map紀錄每個字的順序。
2.將字串兩兩依照map比較,如果發現前面的比後面大就返回false。
3.如果檢查完所有都合法則返回true。
Java: