原题链接在这里: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.

最新文章

  1. 解决服务器SID引起虚拟机不能加入AD域用户,无法远程登录的问题
  2. Swift: 在Swift中桥接OC文件(自己创建的类文件、第三方库文件)
  3. Android热修复之微信Tinker使用初探
  4. c++父类和子类转化致命的代码错误
  5. c语言读取字符在记事本中出现次数
  6. visual studio 2013 快捷键大全
  7. 通过Java反射来理解泛型的本质
  8. jQuery 遍历json数组的实现代码
  9. Cv运动分析与对象跟踪(转)
  10. 获取apk信息工具(android SDK的aapt工具)
  11. js 强制转换
  12. 多名Uber司机被指刷单遭封号 一周薪水为0
  13. 在Button上、下、左、右位置加入图片和文字
  14. 大数据时代之hadoop(二):hadoop脚本解析
  15. UVa 10720 - Graph Construction
  16. jQuery入门(一)
  17. Java与算法之(6) - 八皇后问题
  18. phpStudy 切换版本后没有权限的问题
  19. Redis 认识与安装
  20. JS开发工具WebStorm使用快捷键

热门文章

  1. [SOJ #686]抢救(2019-11-7考试)/[洛谷P3625][APIO2009]采油区域
  2. thymeleaf是用于编写html模版的编程语言(工具语言)
  3. -Shell 教程 Bash 脚本 基础语法 MD
  4. FindWindow和FindWindowEx函数使用
  5. vs2015下编译免费开源的jpeg库,ijg的jpeg.lib
  6. Java自学-接口与继承 接口
  7. 并发编程--一堆锁,GIL,同步异步,Event事件
  8. webpack 里的 import, exports 实现原理
  9. Vue -- 项目报错整理(2):IE报错 - ‘SyntaxError:strict 模式下不允许一个属性有多个定义‘ ,基于vue element-ui页面跳转坑的解决
  10. const关键字总结