zoukankan      html  css  js  c++  java
  • [LC] 269. 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 non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.

    Example 1:

    Input:
    [
      "wrt",
      "wrf",
      "er",
      "ett",
      "rftt"
    ]
    
    Output: "wertf"
    

    Example 2:

    Input:
    [
      "z",
      "x"
    ]
    
    Output: "zx"
    

    Example 3:

    Input:
    [
      "z",
      "x",
      "z"
    ] 
    
    Output: "" 
    
    Explanation: The order is invalid, so return "".
    

    Note:

    1. You may assume all letters are in lowercase.
    2. You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
    3. If the order is invalid, return an empty string.
    4. There may be multiple valid order of letters, return any one of them is fine.
    class Solution {
        public String alienOrder(String[] words) {
            StringBuilder sb = new StringBuilder();
            int[] indegree = new int[26];
            int count = 0;
            Map<Character, Set<Character>> map = new HashMap<>();
            for (String word: words) {
                for (char c : word.toCharArray()) {
                    if (indegree[c - 'a'] == 0) {
                        indegree[c - 'a'] += 1;
                        count += 1;   
                    }
                }
            }
            
            // construct graph
            for (int i = 0; i < words.length - 1; i++) {
                String cur = words[i];
                String next = words[i + 1];
                int min = Math.min(cur.length(), next.length());
                for (int j = 0; j < min; j++) {
                    if (cur.charAt(j) != next.charAt(j)) {
                        if (!map.containsKey(cur.charAt(j))) {
                            map.put(cur.charAt(j), new HashSet<>());
                        }
                        if (map.get(cur.charAt(j)).add(next.charAt(j))) {
                            indegree[next.charAt(j) - 'a'] += 1;
                        }
                        break;
                    }
                }
            }
            
            // initial point
            Queue<Character> queue = new LinkedList<>();
            for (int i = 0; i < 26; i++) {
                if (indegree[i] == 1) {
                    queue.offer((char)('a' + i));
                }
            }
            
            while (!queue.isEmpty()) {
                Character cur = queue.poll();
                sb.append(cur);
                if (!map.containsKey(cur)) {
                    continue;
                }
                for (char ch : map.get(cur)) {
                    if (--indegree[ch - 'a'] == 1) {
                        queue.offer(ch);
                    }
                }
            }
            if (sb.length() != count) {
                return "";
            }
            return sb.toString();
        }
    }
  • 相关阅读:
    文件上传以及JS链式结构
    JQUERY事件
    Asp.Net MVC及Web API框架配置会碰到的几个问题及解决方案(转)
    WebSocket
    用户态和核心态的区别
    Visual Studio中的.suo(Solution User Options)文件
    探索Visual Studio生成的.vs文件夹内部结构和作用
    VMware搭建虚拟机服务器
    【图解】用虚拟机做服务器
    .NET C# 创建WebService服务简单的例子
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12521711.html
Copyright © 2011-2022 走看看