LeetCode 425. Word Squares 单词平方

原创 2017年01月03日 09:57:47

[LeetCode] Word Squares 单词平方

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 alphabeta-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).

思路:
*1.自己想的方法是,DFS+pruning,对[“area”,”lead”,”wall”,”lady”,”ball”],让第一行为area,然后第二行为lead,看这两行的前两列是否构成单词平方,不是则break让第二行为wall,是则继续看第三行。这个思路的特点是,对所有可能的组合去一一判断是否是word squares。

*2.这个思路的相对的思路是,按照word squares一步一步的构造,例如:第一行先选wall, 则第二行开头必须是a才能构成word squares,所以只需要尝试以a开头的单词,发现area可以做第二行,然后第三行开头必须是le才能构成word squares,所以只需要尝试以le做前缀的词,发现lead满足条件,然后第四行必须是以lad开头的,所以找到lady,这时可以把找到的word square存起来,再尝试新的。

class Solution {
public:
    vector<vector<string>> wordSquares(vector<string>& words){
    //用hashtable存每个单词所有前缀
    //参考代码:https://discuss.leetcode.com/topic/63387/java-ac-solution-easy-to-understand
    n=words[0].size();
    for(auto&word:words){
        for(int i=0;i<n;i++){
            mm[word.substr(0,i)].push_back(word);
        }
    }
    helper(0);
    return squares;
    }
    int n;
    unordered_map<string,vector<string>> mm;
    vector<string> square(n,string<n,"">);
    vector<vector<string>> squares;

    void helper(int i){
        if(i==n){squares.push_back(square);return;}
        string prefix;
        for(int k=0;k<i;k++)
            prefix+=square[i][k];
        for(string&word:mm[prefix]){
            square[i]=word;
            helper(i+1);
        }
    }
    //当用iterative比较复杂,代码量太大,则用recursive来写。
    /*for(int i=0;i<words.size();i+=){
        square[0]=words[i];
        for(int j=0;j<n;j++){
            for(int k=0;k<j;k++){
        }
        }
    }*/
    }
    };
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode Add to List 425. Word Squares

leetcode   Add to List 425. Word Squares zip(*)  当前列就是下一行的前缀,用这个来遍历, square 为4时输出方阵。 class Solutio...

leetcode 279. Perfect Squares-完美平方数|动态规划

原题链接:279. Perfect Squares 【思路-Java】 采用动态规划实现。用 dp[i] 数组存储第 i 个数的完美平方数。递推式为:dp[i] = Math.max(dp[j] + ...

CodeForces 425D Sereja and Squares

题意: 平面上有n个点 问 最多能组成多少个边与坐标轴平行的正方形 思路: 这是一个通过不断二分查找乱搞的题… 首先枚举左下角 然后分别往上往右找左上角和右下角 这时如果发现边长不想等就通过长...

【LeetCode-面试算法经典-Java实现】【079-Word Search(单词搜索)】

【079-Word Search(单词搜索)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a 2D board and a word, find if th...

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

【139-Word Break(单词拆分)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a string s and a dictionary of wor...

LeetCode 288. Unique Word Abbreviation(单词缩写)

原题网址:https://leetcode.com/problems/unique-word-abbreviation/ An abbreviation of a word follows the...

leetcode 318. Maximum Product of Word Lengths-最长单词积|位运算

原题链接:318. Maximum Product of Word Lengths 【思路-Java、Python】 本题的妙处在于用一个 int 值就能表示26个字母,哪些字母在单词中出现过,哪些字...

LeetCode 126. Word Ladder II(单词梯子)

原题网址:https://leetcode.com/problems/word-ladder-ii/ Given two words (beginWord and endWord), and a ...

LeetCode | Word Ladder II(单词梯II)

Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)