-
Notifications
You must be signed in to change notification settings - Fork 277
/
ValidAlienDictionary.java
47 lines (40 loc) · 1.11 KB
/
ValidAlienDictionary.java
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
class Solution {
// TC : O(n2)
// SC : O(1)
public boolean isAlienSorted(String[] words, String order) {
Map<Character, Integer> map = new HashMap<>();
int weight = 1;
for(char c: order.toCharArray()){
map.put(c, weight);
weight++;
}
for(int i=0;i<words.length -1;i++){
String first = words[i];
String second = words[i+1];
if(!helper(first, second, map)){
return false;
}
}
return true;
}
private boolean helper(String s1, String s2, Map<Character, Integer> map){
int i1=0;
int i2=0;
while(i1 <s1.length() && i2<s2.length()){
if(s1.charAt(i1)!=s2.charAt(i2)){
if(map.get(s1.charAt(i1))< map.get(s2.charAt(i2)) ){
return true;
} else{
return false;
}
}
i1++;
i2++;
}
// s1 ="hello" s2 ="hell"
if(s1.length()>s2.length()){
return false;
}
return true;
}
}