/* Leetcode problem 953: "Verifying an Alien Dictionary". James S. Plank Thu Feb 2 17:15:49 EST 2023 */ #include #include #include #include #include #include #include #include #include #include #include using namespace std; class Solution { public: bool isAlienSorted(vector& words, string order); }; bool Solution::isAlienSorted(vector& words, string order) { string trans; int i, j; /* Create trans. Trans[i-'a'] contains the character that character i should translate to (such as 'w' to 'a' in Example 4). */ trans.resize(26, ' '); for (i = 0; i < order.size(); i++) trans[order[i]-'a'] = 'a' + i; /* Go through the words vector, and convert each word. */ for (i = 0; i < words.size(); i++) { for (j = 0; j < words[i].size(); j++) { words[i][j] = trans[words[i][j]-'a']; } /* Compare each word to the one that precedes it, and if it is smaller, then the words are not in order -- return false. */ if (i > 0 && words[i] < words[i-1]) return false; } /* If you get here without any words out of order, return true. */ return true; } int main() { vector d; string order; int i; Solution s; bool b; while (cin >> order) d.push_back(order); d.pop_back(); b = s.isAlienSorted(d, order); printf("%s\n", (b) ? "True" : "False"); return 0; }