2021-02-26 猜字谜

猜字谜

难度:困难

外国友人仿照中国字谜设计了一个英文版猜字谜小游戏,请你来猜猜看吧。

字谜的迷面 puzzle按字符串形式给出,如果一个单词 word 符合下面两个条件,那么它就可以算作谜底:

单词word中包含谜面 puzzle 的第一个字母。
单词 word 中的每一个字母都可以在谜面puzzle 中找到。
例如,如果字谜的谜面是 "abcdefg",那么可以作为谜底的单词有 "faced", "cabbage", 和 "baggage";而 "beefed"(不含字母 "a")以及 "based"(其中的 "s" 没有出现在谜面中)。
返回一个答案数组 answer,数组中的每个元素 answer[i] 是在给出的单词列表 words中可以作为字谜迷面 puzzles[i] 所对应的谜底的单词数目。

示例:

输入:
words = ["aaaa","asas","able","ability","actt","actor","access"], 
puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"]
输出:[1,1,3,2,4,0]
解释:
1 个单词可以作为 "aboveyz" 的谜底 : "aaaa" 
1 个单词可以作为 "abrodyz" 的谜底 : "aaaa"
3 个单词可以作为 "abslute" 的谜底 : "aaaa", "asas", "able"
2 个单词可以作为 "absoryz" 的谜底 : "aaaa", "asas"
4 个单词可以作为 "actresz" 的谜底 : "aaaa", "asas", "actt", "access"
没有单词可以作为 "gaswxyz" 的谜底,因为列表中的单词都不含字母 'g'。

提示:

  • 1 <= words.length <= 10^5
  • 4 <= words[i].length <= 50
  • 1 <= puzzles.length <= 10^4
  • puzzles[i].length == 7
  • words[i][j], puzzles[i][j]都是小写英文字母。
  • 每个puzzles[i]所包含的字符都不重复。

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


题解:

  • 思路:
    关键点在于二进制状态压缩。

  • 代码:

public List<Integer> findNumOfValidWordsV1(String[] words, String[] puzzles) {
        List<Integer> ans = new LinkedList<>();
        for(int i = 0; i < puzzles.length; i++) {
            int count = 0;
            for (int j = 0; j < words.length; j++) {
                //word包含puzzle的首字母
                boolean condition1 = words[j].contains(puzzles[i].subSequence(0, 1));
                //word中的每一个字母都能在puzzle中找到
                boolean condition2 = false;
                char[] wordChar = words[j].toCharArray();
                int temp = wordChar.length;
                for (char wc: wordChar) {
                    if (puzzles[i].contains(wc + "")) {
                        temp--;
                    }
                }
                if (temp == 0) {
                    condition2 = true;
                }
                if (condition1 && condition2) {
                    count++;
                }
            }
            ans.add(i, count);
        }
        return ans;
    }

    public static List<Integer> findNumOfValidWordsV2(String[] words, String[] puzzles) {
        List<Integer> ans = new LinkedList<>();
        List<Set<Character>> list = new ArrayList<>();
        for (String word: words) {
            char[] arrays = word.toCharArray();
            Set<Character> set = new HashSet<>();
            for (char c: arrays) {
                set.add(c);
            }
            list.add(set);
        }

        for(int i = 0; i < puzzles.length; i++) {
            int count = 0;
            for (int j = 0; j < list.size(); j++) {
                Set<Character> set = list.get(j);

                //word包含puzzle的首字母
                boolean condition1 = set.contains(puzzles[i].charAt(0));
                //word中的每一个字母都能在puzzle中找到
                boolean condition2 = false;

                int temp = set.size();
                if (temp > 7) {
                    break;
                }
                for (char wc: set) {
                    if (puzzles[i].contains(wc + "")) {
                        temp--;
                    }
                }
                if (temp == 0) {
                    condition2 = true;
                }
                if (condition1 && condition2) {
                    count++;
                }
            }
            ans.add(i, count);
        }
        return ans;
    }

    public static List<Integer> findNumOfValidWordsV3(String[] words, String[] puzzles) {
        Map<Integer, Integer> map = new HashMap<>();
        for (String word: words) {
            int mask = 0;
            char[] chars = word.toCharArray();
            for (char c: chars) {
                mask |= (1 << (c - 'a'));
            }
            if (Integer.bitCount(mask) <= 7) {
                map.put(mask, map.getOrDefault(mask, 0) + 1);
            }
        }

        List<Integer> ans = new ArrayList<Integer>();
        for (String puzzle : puzzles) {
            int total = 0;

            int mask = 0;
            for (int i = 1; i < 7; ++i) {
                mask |= (1 << (puzzle.charAt(i) - 'a'));
            }
            int subset = mask;
            do {
                int s = subset | (1 << (puzzle.charAt(0) - 'a'));
                if (map.containsKey(s)) {
                    total += map.get(s);
                }
                subset = (subset - 1) & mask;
            } while (subset != mask);

            ans.add(total);
        }
        return ans;
    }

    public static void main(String[] args) {
        String[] words = {"aaaa","asas","able","ability","actt","actor","access"};
        String[] puzzles = {"aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"};
        System.err.println(findNumOfValidWordsV3(words, puzzles));
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容