269. Alien Dictionary

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了269. Alien Dictionary相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%8d%95%e8%af%8d' title='鍗曡瘝'>鍗曡瘝   fir   letters   phi   ase   return   queue   bsp   鎷撴墤鎺掑簭   

闂鎻忚堪锛?/p>

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.

 

瑙i鎬濊矾锛?/p>

缁欎簡鎴戜滑涓€涓瓧鍏革紝瀛楀吀鏄寜鐓у瓧姣嶅厛鍚庨『搴忔帓搴忕殑銆?/p>

鐓ц繖涓€鐐癸紝鎴戜滑鍙互瀵绘壘瀛楁瘝涓庡瓧姣嶄箣闂寸殑鍏崇郴銆?/p>

鍙互鏍规嵁瀛楀吀鏋勫缓涓€涓浘锛屾瘡涓瓧姣嶉兘鏄竴涓妭鐐? 鍦ㄥ墠闈㈢殑瀛楁瘝鎸囧悜鍦ㄥ悗闈㈢殑瀛楁瘝锛岃繖鏍峰彲浠ユ瀯鎴愪竴涓湁鍚戝浘锛岄偅涔堟垜浠鍋氱殑灏辨槸姹傝繖涓浘鐨勬嫇鎵戞帓搴忋€?/p>

1.鏋勫缓鍥撅細

瀵瑰瓧鍏镐腑鍑虹幇鐨勬瘡涓€涓瓧姣嶏紝鍒濆鍖栧畠鐨勫叆搴︿负0.

瀵瑰瓧鍏镐腑鐨勬瘡涓€涓崟璇嶏紝鎴戜滑鐢ㄥ畠涓庡畠鍚庨潰涓€涓崟璇嶇浉姣旇緝锛屾壘鍒扮涓€涓笉鐩哥瓑鐨勫瓧姣嶏紝灏唚ords[i][j]鎸囧悜words[i+1][j]锛堝姞鍏ュ埌閭绘帴閾捐〃涓級锛屽悓鏃跺鍔爓ords[i+1][j]鐨勫叆搴︺€?/p>

2.鎷撴墤鎺掑簭锛?/p>

鐜板皢鍏ュ害涓?鐨勬斁鍏ラ槦鍒椾腑

鍙栧嚭闃熼鍏冪礌鏀惧叆杩斿洖瀛楃涓蹭腑锛屾牴鎹偦鎺ラ摼琛ㄥ皢鍏堕偦鎺ョ偣鐨勫叆搴﹀噺1锛岃嫢姝ゆ椂閭绘帴鐐圭殑鍏ュ害涓?锛屽垯灏嗚鐐瑰姞鍏ラ槦鍒椼€?/p>

3.妫€鏌ユ槸鍚︽湁鐜細

鑻ヨ繑鍥炲瓧绗︿覆鐨勯暱搴︿笉绛変簬indegree涓妭鐐圭殑闀垮害锛屽垯璇存槑鏈夌幆锛屾竻绌簉et銆?/p>

 

浠g爜锛?/p>

class Solution {
public:
    string alienOrder(vector<string>& words) {
        string ret;
        unordered_map<char, vector<char>> graph;
        unordered_map<char, int> in_degree;
        for(auto w : words){
            for(auto c : w){
                in_degree[c] = 0;
            }
        }
        for(int i = 0; i < words.size()-1; i++){
            if(words[i][0] != words[i+1][0]){
                graph[words[i][0]].push_back(words[i+1][0]);
                in_degree[words[i+1][0]]++;
            }else{
                int j = 0;
                int len = min(words[i].size(), words[i+1].size());
                while(j < len && words[i][j] == words[i+1][j]) j++;
                if(j != len){
                    graph[words[i][j]].push_back(words[i+1][j]);
                    in_degree[words[i+1][j]]++;
                }
            }
        }
        queue<char> q;
        for(auto p : in_degree){
            if(p.second == 0) q.push(p.first);
        }
        while(!q.empty()){
            char cur = q.front();
            q.pop();
            ret.push_back(cur);
            if(graph.count(cur)){
                for(auto c : graph[cur]){
                    in_degree[c]--;
                    if(in_degree[c] == 0) q.push(c);
                }
            }
        }
        if(ret.size() != in_degree.size()) ret.clear();
        return ret;
        
    }
};

 

以上是关于269. Alien Dictionary的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 269: Alien Dictionary

[LeetCode] 269. Alien Dictionary 外文字典

leetcode269 - Alien Dictionary - hard

269. Alien Dictionary

269. Alien Dictionary

269. Alien Dictionary