GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/java/953_Verifying_an_Alien_Dictionary.java at master · ffortunado/leetcode Expand file tree/ Copy path953_Verifying_an_Alien_Dictionary.javaMore file actions 25 lines (24 loc) · 908 Bytes / Copy path953_Verifying_an_Alien_Dictionary.javaFile metadata and controls25 lines (24 loc) · 908 Bytes 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. |