GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/java/953_Verifying_an_Alien_Dictionary.java at master · qiyuangong/leetcode

Expand file tree

/

953_Verifying_an_Alien_Dictionary.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
25 lines (24 loc) · 908 Bytes
/

953_Verifying_an_Alien_Dictionary.java

Copy path

File metadata and controls

25 lines (24 loc) · 908 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
class Solution {
HashMap<Character, Integer> orderMap = new HashMap<>();
public boolean isAlienSorted(String[] words, String order) {
// Put value index map into hashmap
for (int i = 0; i < order.length(); i++) {
orderMap.put(order.charAt(i), i);
}
for (int i = 0; i < words.length - 1; i++) {
if (cmp_alien(words[i], words[i + 1]) > 0) return false;
}
return true;
}
private int cmp_alien(String a, String b) {
int ls = a.length() < b.length() ? a.length(): b.length();
int pos = 0;
// Compare based on hashmap
while (pos < ls) {
if (orderMap.get(a.charAt(pos)) != orderMap.get(b.charAt(pos)))
return orderMap.get(a.charAt(pos)) - orderMap.get(b.charAt(pos));
pos += 1;
}
return a.length() <= b.length() ? -1: 1;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.