返回介绍

solution / 0200-0299 / 0269.Alien Dictionary / README_EN

发布于 2024-06-17 01:04:02 字数 7114 浏览 0 评论 0 收藏 0

269. Alien Dictionary

中文文档

Description

There is a new alien language that uses the English alphabet. However, the order of the letters is unknown to you.

You are given a list of strings words from the alien language's dictionary. Now it is claimed that the strings in words are sorted lexicographically by the rules of this new language.

If this claim is incorrect, and the given arrangement of string in words cannot correspond to any order of letters, return "".

Otherwise, return _a string of the unique letters in the new alien language sorted in lexicographically increasing order by the new language's rules__. _If there are multiple solutions, return_ any of them_.

 

Example 1:

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

Example 2:

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

Example 3:

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

 

Constraints:

  • 1 <= words.length <= 100
  • 1 <= words[i].length <= 100
  • words[i] consists of only lowercase English letters.

Solutions

Solution 1

class Solution:
  def alienOrder(self, words: List[str]) -> str:
    g = [[False] * 26 for _ in range(26)]
    s = [False] * 26
    cnt = 0
    n = len(words)
    for i in range(n - 1):
      for c in words[i]:
        if cnt == 26:
          break
        o = ord(c) - ord('a')
        if not s[o]:
          cnt += 1
          s[o] = True
      m = len(words[i])
      for j in range(m):
        if j >= len(words[i + 1]):
          return ''
        c1, c2 = words[i][j], words[i + 1][j]
        if c1 == c2:
          continue
        o1, o2 = ord(c1) - ord('a'), ord(c2) - ord('a')
        if g[o2][o1]:
          return ''
        g[o1][o2] = True
        break
    for c in words[n - 1]:
      if cnt == 26:
        break
      o = ord(c) - ord('a')
      if not s[o]:
        cnt += 1
        s[o] = True

    indegree = [0] * 26
    for i in range(26):
      for j in range(26):
        if i != j and s[i] and s[j] and g[i][j]:
          indegree[j] += 1
    q = deque()
    ans = []
    for i in range(26):
      if s[i] and indegree[i] == 0:
        q.append(i)
    while q:
      t = q.popleft()
      ans.append(chr(t + ord('a')))
      for i in range(26):
        if s[i] and i != t and g[t][i]:
          indegree[i] -= 1
          if indegree[i] == 0:
            q.append(i)
    return '' if len(ans) < cnt else ''.join(ans)
class Solution {

  public String alienOrder(String[] words) {
    boolean[][] g = new boolean[26][26];
    boolean[] s = new boolean[26];
    int cnt = 0;
    int n = words.length;
    for (int i = 0; i < n - 1; ++i) {
      for (char c : words[i].toCharArray()) {
        if (cnt == 26) {
          break;
        }
        c -= 'a';
        if (!s[c]) {
          ++cnt;
          s[c] = true;
        }
      }
      int m = words[i].length();
      for (int j = 0; j < m; ++j) {
        if (j >= words[i + 1].length()) {
          return "";
        }
        char c1 = words[i].charAt(j), c2 = words[i + 1].charAt(j);
        if (c1 == c2) {
          continue;
        }
        if (g[c2 - 'a'][c1 - 'a']) {
          return "";
        }
        g[c1 - 'a'][c2 - 'a'] = true;
        break;
      }
    }
    for (char c : words[n - 1].toCharArray()) {
      if (cnt == 26) {
        break;
      }
      c -= 'a';
      if (!s[c]) {
        ++cnt;
        s[c] = true;
      }
    }

    int[] indegree = new int[26];
    for (int i = 0; i < 26; ++i) {
      for (int j = 0; j < 26; ++j) {
        if (i != j && s[i] && s[j] && g[i][j]) {
          ++indegree[j];
        }
      }
    }
    Deque<Integer> q = new LinkedList<>();
    for (int i = 0; i < 26; ++i) {
      if (s[i] && indegree[i] == 0) {
        q.offerLast(i);
      }
    }
    StringBuilder ans = new StringBuilder();
    while (!q.isEmpty()) {
      int t = q.pollFirst();
      ans.append((char) (t + 'a'));
      for (int i = 0; i < 26; ++i) {
        if (i != t && s[i] && g[t][i]) {
          if (--indegree[i] == 0) {
            q.offerLast(i);
          }
        }
      }
    }
    return ans.length() < cnt ? "" : ans.toString();
  }
}
class Solution {
public:
  string alienOrder(vector<string>& words) {
    vector<vector<bool>> g(26, vector<bool>(26));
    vector<bool> s(26);
    int cnt = 0;
    int n = words.size();
    for (int i = 0; i < n - 1; ++i) {
      for (char c : words[i]) {
        if (cnt == 26) break;
        c -= 'a';
        if (!s[c]) {
          ++cnt;
          s[c] = true;
        }
      }
      int m = words[i].size();
      for (int j = 0; j < m; ++j) {
        if (j >= words[i + 1].size()) return "";
        char c1 = words[i][j], c2 = words[i + 1][j];
        if (c1 == c2) continue;
        if (g[c2 - 'a'][c1 - 'a']) return "";
        g[c1 - 'a'][c2 - 'a'] = true;
        break;
      }
    }
    for (char c : words[n - 1]) {
      if (cnt == 26) break;
      c -= 'a';
      if (!s[c]) {
        ++cnt;
        s[c] = true;
      }
    }
    vector<int> indegree(26);
    for (int i = 0; i < 26; ++i)
      for (int j = 0; j < 26; ++j)
        if (i != j && s[i] && s[j] && g[i][j])
          ++indegree[j];
    queue<int> q;
    for (int i = 0; i < 26; ++i)
      if (s[i] && indegree[i] == 0)
        q.push(i);
    string ans = "";
    while (!q.empty()) {
      int t = q.front();
      ans += (t + 'a');
      q.pop();
      for (int i = 0; i < 26; ++i)
        if (i != t && s[i] && g[t][i])
          if (--indegree[i] == 0)
            q.push(i);
    }
    return ans.size() < cnt ? "" : ans;
  }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文