[LeetCode] 425. Word Squares

Problem

Given a set of words (without duplicates), find all word squares you can build from them.

A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).

For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically.

b a l l
a r e a
l e a d
l a d y

Note:
There are at least 1 and at most 1000 words.
All words will have the exact same length.
Word length is at least 1 and at most 5.
Each word contains only lowercase English alphabet a-z.
Example 1:

Input:
["area","lead","wall","lady","ball"]

Output:
[
  [ "wall",
    "area",
    "lead",
    "lady"
  ],
  [ "ball",
    "area",
    "lead",
    "lady"
  ]
]

Explanation:
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).
Example 2:


Input:
["abat","baba","atan","atal"]

Output:
[
  [ "baba",
    "abat",
    "baba",
    "atan"
  ],
  [ "baba",
    "abat",
    "baba",
    "atal"
  ]
]

Explanation:
The output consists of two word squares. The order of output does not matter (just the order of words in each word square matters).

Reference:
https://leetcode.com/problems...

Solution - Trie+DFS

class Solution {
    public List<List<String>> wordSquares(String[] words) {
        List<List<String>> res = new ArrayList<>();
        if (words == null || words.length == 0) return res;
        int len = words[0].length();
        List<String> temp = new ArrayList<>();
        Trie trie = new Trie(words);
        for (String word: words) {
            temp.add(word);
            dfs(trie, len, temp, res);
            temp.remove(temp.size()-1);
        }
        return res;
    }
    
    private void dfs(Trie trie, int len, List<String> temp, List<List<String>> res) {
        int size = temp.size();
        if (size == len) {
            res.add(new ArrayList<>(temp));
            return;
        }
        StringBuilder sb = new StringBuilder();
        for (String str: temp) {
            sb.append(str.charAt(size));
        }
        String prefix = sb.toString();
        List<String> words = trie.getWords(prefix);
        for (String word: words) {
            temp.add(word);
            dfs(trie, len, temp, res);
            temp.remove(temp.size()-1);
        }
    }
}

class Trie {
    TrieNode root;
    public Trie(String[] strs) {
        root = new TrieNode();
        for (String str: strs) {
            TrieNode cur = root;
            for (char ch: str.toCharArray()) {
                int index = ch-'a';
                if (cur.children[index] == null) cur.children[index] = new TrieNode();
                cur.children[index].words.add(str);
                cur = cur.children[index];
            }
        }
    }
    
    public List<String> getWords(String prefix) {
        List<String> res = new ArrayList<>();
        TrieNode cur = root;
        for (char ch: prefix.toCharArray()) {
            int index = ch-'a';
            if (cur.children[index] == null) return res;
            cur = cur.children[index];
        }
        res.addAll(cur.words);
        return res;
    }
}

class TrieNode {
    TrieNode[] children;
    List<String> words;
    public TrieNode() {
        children = new TrieNode[26];
        words = new ArrayList<>();
    }
}

Road to Glory
對酒當歌,人生幾何? 譬如朝露,去日苦多。
161 声望
53 粉丝
0 条评论
推荐阅读
[LeetCode] 958. Check Completeness of a Binary Tree
Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possibl...

linspiration阅读 1.9k

Java12的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft63阅读 12k

Java8的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft32阅读 24.7k评论 1

一文搞懂秒杀系统,欢迎参与开源,提交PR,提高竞争力。早日上岸,升职加薪。
前言秒杀和高并发是面试的高频考点,也是我们做电商项目必知必会的场景。欢迎大家参与我们的开源项目,提交PR,提高竞争力。早日上岸,升职加薪。知识点详解秒杀系统架构图秒杀流程图秒杀系统设计这篇文章一万多...

王中阳Go35阅读 2.6k评论 1

封面图
Java11的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft28阅读 15.5k评论 3

Java5的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft13阅读 20.5k

Java9的新特性
Java语言特性系列Java5的新特性Java6的新特性Java7的新特性Java8的新特性Java9的新特性Java10的新特性Java11的新特性Java12的新特性Java13的新特性Java14的新特性Java15的新特性Java16的新特性Java17的新特性Java...

codecraft20阅读 14.6k

161 声望
53 粉丝
宣传栏