Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit13fc282

Browse files
authored
Update Verifying an Alien Dictionary.java
1 parentc5acea2 commit13fc282

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
classSolution {
22
publicbooleanisAlienSorted(String[]words,Stringorder) {
3-
Map<Character,Integer>dictionary =newHashMap<>();
4-
for (inti =0;i <order.length();i++) {
5-
dictionary.put(order.charAt(i),i);
3+
Map<Character,Integer>map =newHashMap<>();
4+
intidx =0;
5+
for (charc :order.toCharArray()) {
6+
map.put(c,idx++);
67
}
7-
for (inti =0;i <words.length -1;i++) {
8-
if (!hasCorrectOrder(words[i],words[i +1],dictionary)) {
8+
for (inti =1;i <words.length;i++) {
9+
if (!isSorted(words[i -1],words[i],map)) {
910
returnfalse;
1011
}
1112
}
1213
returntrue;
1314
}
1415

15-
privatebooleanhasCorrectOrder(StringfirstWord,StringsecondWord,Map<Character,Integer>dictionary) {
16-
intidxOne =0;
17-
intidxTwo =0;
18-
booleancorrectOrder =false;
19-
while (idxOne <firstWord.length() &&idxTwo <secondWord.length()) {
20-
intdictionaryDiff =dictionary.get(firstWord.charAt(idxOne++)) -dictionary.get(secondWord.charAt(idxTwo++));
21-
if (dictionaryDiff >0) {
16+
privatebooleanisSorted(StringwordOne,StringwordTwo,Map<Character,Integer>map) {
17+
for (inti =0;i <Math.min(wordOne.length(),wordTwo.length());i++) {
18+
intdiff =map.get(wordOne.charAt(i)) -map.get(wordTwo.charAt(i));
19+
if (diff >0) {
2220
returnfalse;
23-
}
24-
if (dictionaryDiff <0) {
25-
correctOrder =true;
26-
break;
21+
}elseif (diff <0) {
22+
returntrue;
2723
}
2824
}
29-
returncorrectOrder ||firstWord.length() <=secondWord.length();
25+
returnwordOne.length() <=wordTwo.length();
3026
}
3127
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp