[LeetCode] 422. Valid Word Square_Easy
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:
- The number of words given is at least 1 and does not exceed 500.
- Word length will be at least 1 and does not exceed 500.
- 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的更多相关文章
- LeetCode 422. Valid Word Square
原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...
- 【LeetCode】422. Valid Word Square 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...
- [LeetCode] 408. Valid Word Abbreviation_Easy
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the give ...
- 422. Valid Word Square
似乎可以沿着对角线往右往下检查,也可以正常按题设检查. 我用的后者.. public class Solution { public boolean validWordSquare(List<S ...
- [LeetCode] 367. Valid Perfect Square_Easy tag:Math
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode] Valid Word Square 验证单词平方
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- Leetcode: Valid Word Square
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写
A string such as "word" contains the following abbreviations: ["word", "1or ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
随机推荐
- WordOperate
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using S ...
- 微信小程序学习——框架视图层(view)
视图层是有WXML与WXSS编写的,由组件来进行展示. WXML(WeiXin Markup Language)用于写页面结构的. WXSS(WeiXin Style Sheet)用于页面的样式. 组 ...
- CH 1401 - 兔子与兔子 - [字符串hash]
题目链接:传送门 描述 很久很久以前,森林里住着一群兔子.有一天,兔子们想要研究自己的 DNA 序列.我们首先选取一个好长好长的 DNA 序列(小兔子是外星生物,DNA 序列可能包含 26 个小写英文 ...
- [No0000155]为什么32位机器最大只能用到4GB内存
在此之前先来了解一些计算机存储单位之间的关系以及计算机系统结构和PC硬件方面的一些知识. 一.计算机存储单位之间的关系 ,最小的存储单位. 个二进制位为一个字节(B),即1B = 8bit,最常用的单 ...
- Linux的磁盘系统和文件系统显示的文件大小为什么不一样(du指令和ls指令的区别)
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...
- [ovs] ovs开启日志debug
如题 [root@vrouter1 ~]# ovs-appctl vlog/set netdev:file:dbg [root@vrouter1 ~]# ovs-appctl vlog/set net ...
- Flink - CoGroup
使用方式, dataStream.coGroup(otherStream) .where(0).equalTo(1) .window(TumblingEventTimeWindows.of(Time. ...
- elastic search范围查询
queryBuilder.must(QueryBuilders.rangeQuery("pt_longitude").from(minLongitude).to(maxLongit ...
- mybatis传入某一列的值,然后设置这一列的值是这个
select '${action}' as action from table name parameterType="map"可以指定为map,然后这边就可以用${xxx}来取值 ...
- idea的基本使用
对于Idea没有workspace的概念,但是它把每个项目都看成是maven的一个模块,在一个idea窗口要想显示多个项目时就和eclipse不太一样,下面会详细介绍. 另外maven的setting ...