LeetCode-Alien Dictionary

There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

For example,
Given the following words in dictionary,

[
  "wrt",
  "wrf",
  "er",
  "ett",
  "rftt"
]

The correct order is: "wertf".

Note:

    1. You may assume all letters are in lowercase.
    2. If the order is invalid, return an empty string.
    3. There may be multiple valid order of letters, return any one of them is fine.

Solution:

  

 1 public class Solution {
 2     public void addressTwoWords(List<List<Character>> relSet, int[] preList, String word1, String word2) {
 3         int len = Math.min(word1.length(), word2.length());
 4         boolean relFound = false;
 5         for (int i = 0; i < len; i++) {
 6             char c1 = word1.charAt(i);
 7             char c2 = word2.charAt(i);
 8             if (c1 != c2 && !relFound) {
 9                 if (relSet.get(c1 - 'a') == null) {
10                     relSet.set(c1 - 'a', new ArrayList<Character>());
11                 }
12                 relSet.get(c1 - 'a').add(c2);
13                 preList[c2 - 'a'] = (preList[c2 - 'a'] == -1) ? 1 : preList[c2 - 'a'] + 1;
14                 relFound = true;
15             } else {
16                 if (preList[c2 - 'a'] == -1)
17                     preList[c2 - 'a'] = 0;
18             }
19         }
20         int len2 = Math.max(word1.length(), word2.length());
21         for (int i = len; i < len2; i++) {
22             if (i < word2.length() && preList[word2.charAt(i) - 'a'] == -1)
23                 preList[word2.charAt(i) - 'a'] = 0;
24         }
25     }
26 
27     public String alienOrder(String[] words) {
28         if (words.length == 0)
29             return "";
30         if (words.length == 1)
31             return words[0];
32 
33         List<List<Character>> relSet = new ArrayList<List<Character>>();
34         int[] preList = new int[26];
35         for (int i = 0; i < 26; i++) {
36             relSet.add(null);
37             preList[i] = -1;
38         }
39         StringBuilder builder = new StringBuilder();
40 
41         addressTwoWords(relSet, preList, "", words[0]);
42         for (int i = 0; i < words.length - 1; i++) {
43             addressTwoWords(relSet, preList, words[i], words[i + 1]);
44         }
45 
46         // Topological sort
47         HashSet<Character> set = new HashSet<Character>();
48         for (int i = 0; i < 26; i++)
49             if (preList[i] == 0) {
50                 char c = (char)('a' + i);
51                 set.add(c);
52             }
53 
54         while (!set.isEmpty()) {
55             Character c = set.iterator().next();
56             builder.append(c);
57             List<Character> sList = relSet.get(c - 'a');
58             if (sList != null)
59                 for (int i = 0; i < sList.size(); i++) {
60                     preList[sList.get(i) - 'a']--;
61                     if (preList[sList.get(i) - 'a'] == 0)
62                         set.add(sList.get(i));
63                 }
64             set.remove(c);
65         }
66         for (int i = 0; i < 26; i++)
67             if (preList[i] > 0)
68                 return "";
69         return builder.toString();
70     }
71 }

 

转载于:https://www.cnblogs.com/lishiblog/p/5681041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值