Skip to content

Commit 4f3be67

Browse files
authored
Create 953. Verifying an Alien Dictionary
1 parent 9d8d185 commit 4f3be67

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

953. Verifying an Alien Dictionary

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public boolean isAlienSorted(String[] words, String order) {
3+
int[] mapping = new int[26];
4+
int seq=0;
5+
for(char ch:order.toCharArray()){
6+
mapping[ch-'a'] = seq++;
7+
}
8+
9+
for(int i=0;i<words.length - 1;i++){
10+
String curr = words[i];
11+
String next = words[i+1];
12+
13+
int len = Math.min(curr.length(),next.length());
14+
15+
if(len!= curr.length() && len == next.length() && curr.startsWith(next)){
16+
return false;
17+
}
18+
19+
for(int l=0;l<len;l++){
20+
if(mapping[curr.charAt(l)-'a'] > mapping[next.charAt(l)-'a']){
21+
return false;
22+
}
23+
24+
if(mapping[curr.charAt(l)-'a'] < mapping[next.charAt(l)-'a']){
25+
break;
26+
}
27+
28+
}
29+
}
30+
31+
return true;
32+
}
33+
}

0 commit comments

Comments
 (0)