소문자 알파벳으로 이루어진 단어들이 담긴 문자 벡터와
일반적인 알파벳 순서가 아닌 다른 순서를 알려주는 order 문자열을 받는다.
문제는 벡터 내의 단어 순서가 order 순서로 정렬되어 있는지 판단하는 것이다.
class Solution {
public:
bool isAlienSorted(vector<string>& words, string order) {
unordered_map<char, int> dictionary{};
int count{0};
for (char& c : order)
{
dictionary[c] = ++count;
}
int length = words.size() - 1;
for (int i = 0; i < length; ++i)
{
int leftLength = words[i].size();
int rightLength = words[i + 1].size();
int index{0};
while (index < leftLength && index < rightLength)
{
if (dictionary[words[i][index]] == dictionary[words[i + 1][index]])
{
++index;
continue;
}
else if (dictionary[words[i][index]] < dictionary[words[i + 1][index]])
{
break;
}
else
{
return false;
}
}
if (index == rightLength && rightLength < leftLength)
{
return false;
}
}
return true;
}
};