-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution953.cs
48 lines (39 loc) · 1.08 KB
/
Solution953.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Text;
using LeetCode.DataStructures;
namespace LeetCode.Solutions;
public class Solution953
{
/// <summary>
/// 953. Verifying an Alien Dictionary - Easy
/// <a href="https://leetcode.com/problems/verifying-an-alien-dictionary">See the problem</a>
/// </summary>
public bool IsAlienSorted(string[] words, string order)
{
var orderMap = new int[26];
for (var i = 0; i < order.Length; i++)
{
orderMap[order[i] - 'a'] = i;
}
for (var i = 1; i < words.Length; i++)
{
if (!IsSorted(words[i - 1], words[i], orderMap))
{
return false;
}
}
return true;
}
private bool IsSorted(string word1, string word2, int[] orderMap)
{
var i = 0;
while (i < word1.Length && i < word2.Length)
{
if (word1[i] != word2[i])
{
return orderMap[word1[i] - 'a'] < orderMap[word2[i] - 'a'];
}
i++;
}
return word1.Length <= word2.Length;
}
}