ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

269. 火星词典

2022-01-07 10:31:31  阅读:193  来源: 互联网

标签:index 顺序 int 字母 length words 269 火星 词典


现有一种使用英语字母的火星语言,这门语言的字母顺序与英语顺序不同。

给你一个字符串列表 words ,作为这门语言的词典,words 中的字符串已经 按这门新语言的字母顺序进行了排序 。

请你根据该词典还原出此语言中已知的字母顺序,并 按字母递增顺序 排列。若不存在合法字母顺序,返回 "" 。若存在多种可能的合法字母顺序,返回其中 任意一种 顺序即可。

字符串 s 字典顺序小于 字符串 t 有两种情况:

在第一个不同字母处,如果 s 中的字母在这门外星语言的字母顺序中位于 t 中字母之前,那么 s 的字典顺序小于 t 。
如果前面 min(s.length, t.length) 字母都相同,那么 s.length < t.length 时,s 的字典顺序也小于 t 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/alien-dictionary
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

import java.util.*;

class Solution {
    public String alienOrder(String[] words) {
        Map<Character, Set<Character>> map = new HashMap<>();
        for (int i = 0; i < words.length - 1; i++) {
            int index = 0;
            int length = Math.max(words[i].length(), words[i + 1].length());
            while (index < length) {
                if (index == words[i + 1].length()) {
                    return "";
                }

                if (index == words[i].length()) {
                    break;
                }

                if (words[i].charAt(index) != words[i + 1].charAt(index)) {
                    map.computeIfAbsent(words[i].charAt(index), k -> new HashSet<>()).add(words[i + 1].charAt(index));
                    break;
                }
                index++;
            }
        }

        int[] in = new int[26];
        Arrays.fill(in, -1);

        for (String str : words) {
            //将出现过的字母的入度初始化为0
            for (char ch : str.toCharArray()) {
                in[ch - 'a'] = 0;
            }
        }

        for (Map.Entry<Character, Set<Character>> entry : map.entrySet()) {
            Set<Character> value = entry.getValue();
            for (Character character : value) {
                in[character - 'a']++;
            }
        }

        Queue<Character> queue = new LinkedList<>();

        /**
         * 环检测
         * 入队的节点数等于节点总数
         */
        int nodesCount = 0;
        for (int i = 0; i < in.length; i++) {
            if (in[i] != -1) {
                nodesCount++;
            }
            if (in[i] == 0) {
                queue.offer((char) (i + 'a'));
            }
        }

        StringBuilder stringBuilder = new StringBuilder();
        while (!queue.isEmpty()) {
            Character character = queue.poll();
            stringBuilder.append(character);
            Set<Character> toSet = map.getOrDefault(character, Collections.emptySet());
            if (toSet.size() != 0) {
                for (Character to : toSet) {
                    in[to - 'a']--;
                    if (in[to - 'a'] == 0) {
                        queue.offer(to);
                    }
                }
            }
        }

        return stringBuilder.length() == nodesCount ? stringBuilder.toString() : "";
    }
}

标签:index,顺序,int,字母,length,words,269,火星,词典
来源: https://www.cnblogs.com/tianyiya/p/15773987.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有