[LeetCode] 422. Valid Word Square_Easy

时间:2022-09-02 14:08:20

Given a sequence of words, check whether it forms a valid word square.

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

Note:

  1. The number of words given is at least 1 and does not exceed 500.
  2. Word length will be at least 1 and does not exceed 500.
  3. Each word contains only lowercase English alphabet a-z.

Example 1:

Input:
[
"abcd",
"bnrt",
"crmy",
"dtye"
] Output:
true Explanation:
The first row and first column both read "abcd".
The second row and second column both read "bnrt".
The third row and third column both read "crmy".
The fourth row and fourth column both read "dtye". Therefore, it is a valid word square.

Example 2:

Input:
[
"abcd",
"bnrt",
"crm",
"dt"
] Output:
true Explanation:
The first row and first column both read "abcd".
The second row and second column both read "bnrt".
The third row and third column both read "crm".
The fourth row and fourth column both read "dt". Therefore, it is a valid word square.

Example 3:

Input:
[
"ball",
"area",
"read",
"lady"
] Output:
false Explanation:
The third row reads "read" while the third column reads "lead". Therefore, it is NOT a valid word square.

思路是有可能words里面的word长度不一致, 就无法正常比较,比如 words = ["abcd","bnrt","crmy","de"] , 那么 temp = list(map("".join, zip(*words))) 只等于 ['abcd', 'bnre'], 所以要将words里面不足长度的用" " 来补齐.

Code

class Solution:
def validWordSquare(self, words):
n = len(words)
for index, word in enumerate(words):
if len(word) > n:
return False
if len(word) < n:
words[index] += ' '*(n - len(word))
temp = list(map("".join, zip(*words)))
return temp == words

[LeetCode] 422. Valid Word Square_Easy的更多相关文章

  1. LeetCode 422&period; Valid Word Square

    原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...

  2. 【LeetCode】422&period; Valid Word Square 解题报告&lpar;C&plus;&plus;&rpar;

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...

  3. &lbrack;LeetCode&rsqb; 408&period; Valid Word Abbreviation&lowbar;Easy

    Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...

  4. 422&period; Valid Word Square

    似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...

  5. &lbrack;LeetCode&rsqb; 367&period; Valid Perfect Square&lowbar;Easy tag&colon;Math

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  6. &lbrack;LeetCode&rsqb; Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  7. Leetcode&colon; Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  8. &lbrack;LeetCode&rsqb; Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

  9. &lbrack;LeetCode&rsqb; Longest Valid Parentheses

    第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...

随机推荐

  1. 005&lowbar;kafka&lowbar;Java&lowbar;API

    1.生产者Producer 1)添加依赖 <dependency> <groupId>org.apache.kafka</groupId> <artifact ...

  2. 访问iPhone通讯录的问题

    每个APP只显示一次上图的提示,即使卸载APP也不显示.如果想再次显示提示,可进入 设置-->通用-->还原-->还原位置与隐私,即可. 而且在ios6中文环境下,访问通讯录会出现错 ...

  3. Hdu 4311-Meeting point-1 曼哈顿距离&comma;前缀和

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4311 Meeting point-1 Time Limit: 2000/1000 MS (Java/Oth ...

  4. copy&lpar;source&comma;destination&rpar;拷贝文件

    source 必须 规定要复制的文件 destination 复制文件的目的地 说明 :将文件从 source 拷贝到 destination.如果成功则返回 TRUE,否则返回 FALSE. 例如: ...

  5. Android学习笔记--Broadcast&comma; BroadcastReceiver(广播)

    参考资料:http://www.cnblogs.com/playing/archive/2011/03/23/1992030.html 在 Android 中使用 Activity, Service, ...

  6. 【转】Configuring VM Acceleration on Linux

    Configuring VM Acceleration on Linux Linux-based systems support virtual machine acceleration throug ...

  7. dedecms文档长尾关键词,根据频率先替换长尾关键词再替换主关键词

    问题:在使用dedecms的文档关键词替换功能时,会造成交叉替换 如: 长尾关键词1:男人喝金丝皇菊有什么好处? 长尾关键词2:喝金丝皇菊有什么好处? 主关键词3:金丝皇菊 使用系统默认的功能时,在替 ...

  8. jquery获取select多选框选中的值

    select下拉框选中的值,用jquery大家应该都会获取, $("#selectBox option:selected").val(); 如果select是多选的,也这么获取的话 ...

  9. python数据结构与算法第八天【冒泡排序】

    1.排序算法的稳定性 稳定排序算法会让原本有相同键值的记录维持相对次序 例如:对以下元组按照元组的第一个元素升序排列,元组如下: (4,1) (3,1) (3,7) (5,6) 若要满足条件,则可能的 ...

  10. hdu 6319 Problem A&period; Ascending Rating &lpar;2018 Multi-University Training Contest 3 A&rpar;

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=6319 思路: 单调队列倒着维护,队列里面剩下的值的数量就是这一段区间的count值,如样例第一个区间:3 ...