Raiha173
Member
Cách chuyển thành normal hay nhỉ, mình làm duyệt thấy cùi cùi
C#:
public static bool IsAlienSorted(string[] words, string order)
{
var dict = new Dictionary<char, int>();
for (var index = 0; index < order.Length; index++)
{
dict.Add(order[index], index);
}
for (var i = 0; i < words.Length - 1; i++)
{
if (dict[words[i][0]] < dict[words[i+1][0]]) continue;
var minLength = Math.Min(words[i].Length, words[i + 1].Length);
for (var index = 0; index < minLength; index++)
{
var dif = dict[words[i][index]] - dict[words[i + 1][index]];
if (dif > 0) return false;
if (dif < 0) break;
if (index == minLength -1 && words[i].Length > words[i + 1].Length) return false;
}
}
return true;