原题链接在这里:https://leetcode.com/problems/valid-word-square/

题目:

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.

题解:

检查是否关于对角线对称.

每一个char 都要检查一遍. 这里注意内层loop j 不是从 i 开始而是从0开始.

原本想只检查右上部分在左下部分是否有对称即可,但忽略了这里不一定size是对称的,如果左下有这个char而右上没有这个char就不能检测出false.

Time Complexity: O(m * n). m = words.size(). n 是最长string的length.

Space: O(1).

AC Java:

 public class Solution {
public boolean validWordSquare(List<String> words) {
if(words == null || words.size() == 0){
return true;
}
int m = words.size();
for(int i = 0; i<m; i++){
for(int j = 0; j<words.get(i).length(); j++){
if(j >= m || i >= words.get(j).length() || words.get(i).charAt(j) != words.get(j).charAt(i)){
return false;
}
}
}
return true;
}
}

类似Toeplitz Matrix.

LeetCode 422. Valid Word Square的更多相关文章

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

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

  2. [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 v ...

  3. 422. Valid Word Square

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

  4. [LeetCode] Valid Word Square 验证单词平方

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

  5. Leetcode: Valid Word Square

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

  6. [LeetCode] 408. Valid Word Abbreviation_Easy

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

  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] 367. Valid Perfect Square 检验完全平方数

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

  9. Leetcode 367. Valid Perfect Square

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

随机推荐

  1. ipv4的ip字符串转化为int型

    要求: 将现有一个ipv4的ip字符串(仅包含数字,点,空格), 其中数字和点之间的空格(至多一个)是合法的,比如“12 .3. 4 .62”,其他情况均为非法地址.写一个函数将ipv4地址字符串转化 ...

  2. ubuntu 18 docker 搭建Prometheus+Grafana

    Prometheus(普罗米修斯)是一套开源的监控&报警&时间序列数据库的组合,起始是由SoundCloud公司开发的.随着发展,越来越多公司和组织接受采用Prometheus,社会也 ...

  3. 【题解】Luogu P2447 [SDOI2010]外星千足虫

    原题传送门 根据题意,题目给的每个操作就相当于异或上选中的那几只虫子的足数(mod 2)等于0/1 这是一个异或方程组,珂以用高斯消元解出每个虫子的足数(mod 2).所需最小次数或判断有多解 但是看 ...

  4. [SOJ #687]双生串(2019-11-6考试)/[hdu5431]AB String

    题目大意 把所有仅包含\(AB\)的字符串按字典序排列,给你一个仅包含\(AB\)的字符串\(S\),然后有\(Q\)个问题,第\(i\)个问题给你\(k_i\),求不是\(S\)的子串中,第\(k_ ...

  5. 计算标准差——Python

    计算标准差 题目描述: 编写一个函数计算一系列数的标准差.‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪ ...

  6. Javascript文件上传插件

    jQuery File Uploader 这是最受欢迎的 jQuery 文件上传组件,支持批量上传,拖放上传,显示上传进度条以及校验功能. 支持预览图片.音频和视频,支持跨域上传和客户端图片缩放,支持 ...

  7. Spring Security OAuth2.0 - AuthorizationServer和ResourceServer分离

    <Spring Security实现OAuth2.0授权服务 - 基础版>和<Spring Security实现OAuth2.0授权服务 - 进阶版>两篇文章中介绍如何搭建OA ...

  8. IDEA新建一个Spring Boot项目

    Maven构建项目模板 maven构建的是maven风格的纯净模板,要转变成spring boot项目需要自己添加依赖等配置. mvn archetype:generate: Maven插件原型是一个 ...

  9. jQuery简易Ajax(六)

    一.jQuery中ajax的两种书写方式[一般采用第二种方式]1.$.ajax(url,[setting]); 2.$.ajax([setting]); setting参数说明:setting为一个对 ...

  10. jquery设置css,animate设置多个属性

    $("p").css("color","red"); $("p").css({ "font-size" ...