-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ_953.cs
53 lines (40 loc) · 1.62 KB
/
Q_953.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
49
50
51
52
53
public class Solution {
public bool IsAlienSorted(string[] words, string order) {
if(words == null) return false;
if(words.Length == 0) return false;
if(order == null) return false;
if(words.Length == 1) return true;
// using the 'order' string, convert a
// word from alien ordering to regular ordering
Dictionary<int, int> actualPos = new Dictionary<int, int>();
for(int x = 0; x < order.Length; x++)
{
actualPos.Add(order[x] - 'a', x);
}
for(int x = 1; x < words.Length; x++)
{
int l1 = words[x-1].Length;
int l2 = words[x].Length;
//if(l1 > l2) return false;
//Console.WriteLine("comparing: " + words[x-1] + " , " + words[x]);
for(int y = 0, z = 0; y < l1 && z < l2; y++, z++)
{
int w1 = words[x-1][y] - 'a';
int w2 = words[x][z] - 'a';
//Console.Write("x: " + x + ", ");
//Console.WriteLine("comparing: " + w1 + " , " + w2);
if(actualPos[w1] > actualPos[w2])
{
return false;
}
else if(actualPos[w1] < actualPos[w2] && (x == words.Length-1))
{
return true;
}
}
//Console.WriteLine("h: " + words[x-1] + " , " + words[x]);
if(l1 > l2) return false;
}
return true;
}
}