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.

这道题给了我们一个二位数组,每行每列都是一个单词,需要满足第k行的单词和第k列的单词要相等,这里不要求每一个单词的长度都一样,只要对应位置的单词一样即可。那么这里实际上也就是一个遍历二维数组,然后验证对应位上的字符是否相等的问题,由于各行的单词长度不一定相等,所以我们在找对应位置的字符时,要先判断是否越界,即对应位置是否有字符存在,遇到不符合要求的地方直接返回false,全部遍历结束后返回true,参见代码如下:

class Solution {
public:
bool validWordSquare(vector<string>& words) {
if (words.empty()) return true;
if (words.size() != words[].size()) return false;
for (int i = ; i < words.size(); ++i) {
for (int j = ; j < words[i].size(); ++j) {
if (j >= words.size() || i >= words[j].size() || words[i][j] != words[j][i]) {
return false;
}
}
}
return true;
}
};

参考资料:

https://discuss.leetcode.com/topic/63387/java-ac-solution-easy-to-understand

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Valid Word Square 验证单词平方的更多相关文章

  1. [LeetCode] Valid Word Abbreviation 验证单词缩写

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

  2. Leetcode: Valid Word Square

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

  3. LeetCode 422. Valid Word Square

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

  4. 【LeetCode】422. Valid Word Square 解题报告(C++)

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

  5. [LeetCode] 140. Word Break II 单词拆分II

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...

  6. LeetCode 126. Word Ladder II 单词接龙 II(C++/Java)

    题目: Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transfo ...

  7. [leetcode]367. Valid Perfect Square验证完全平方数

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

  8. [LeetCode] Valid Tic-Tac-Toe State 验证井字棋状态

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  9. [LeetCode] Valid Perfect Square 检验完全平方数

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

随机推荐

  1. IOS学习之-私人通讯录

    通过一段时间IOS的学习完成了一个简单的应用,"私人通讯录". 运行效果如下图: 1.登录页 2.通讯录列表 3.添加 4.编辑 5.删除 6.注销 总视图结构如下图: 总结本程序 ...

  2. 在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

    在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle.SQLServer.Mysql这些常规的数据库,也有可能访问SQLite.Access,或者一些我们可能不常用的PostgreS ...

  3. [修正] Firemonkey 中英文混排折行问题(移动平台)

    问题:FMX 在移动平台的文字显示并非由该平台的原生 API 来显示,而是由 FMX.TextLayout.GPU 来处理,也许是官方没留意到中文字符的问题,造成在中英文混排折行时,有些问题. 适用: ...

  4. 数据库---实验四 oracle的安全性和完整性控制

    实验内容: (一) 授权 . 以dba用户的身份登陆oracle,创建用户u1+学号后四位,u2+学号后四位. SQL> create user u1_3985 identified by &q ...

  5. PHP中this,self,parent三个关键字

    this,self,parent三个关键字从字面上比较好理解,分别是指这.自己.父亲. this是指向当前对象的指针(姑且用C里面的指针来看吧)self是指向当前类的指针parent是指向父类的指针( ...

  6. 认识Git

    ---恢复内容开始--- Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目. Git作为当下最潮流的版本控制工具也是有他独特的不同,最大的不同就在于他有分布式版本管理的 ...

  7. C# Session添加、删除封装类

    /// <summary> /// <para> </para> /// 常用工具类——Session操作类 /// <para> ---------- ...

  8. ArcGIS Engine开发之地图基本操作(1)

    ArcGIS提供的各类数据形式以及相应接口 1. 空间数据 在GIS软件中,空间数据有多种不同的形式存在.按照不同的划分标准可以分为矢量数据和栅格数据.GIS格式数据和非GIS格式数据(CAD格式). ...

  9. 01背包问题python 2.7实现

    版权声明:本文为博主原创文章,转载请注明转自 http://www.cnblogs.com/kdxb/p/6140625.html #!/usr/bin/env python # -*- coding ...

  10. GCC 预处理、编译、汇编、链接..

    1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...