美文网首页
422-有效的单词方块

422-有效的单词方块

作者: 饮酒醉回忆 | 来源:发表于2019-08-15 15:50 被阅读0次

有效的单词方块

题目

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.

给定一个单词序列,检测它是否构成一个有效的单词正方形.

如果第k行和第k列读取完全相同的字符串,其中0<=k<max(numRows,numColumns),则单词序列形成一个有效的单词平方.

注意:

  1. 给出的单词数置为1 ,且不超过500.

  2. 单词长度将至少为1,且不超过500.

  3. 每个单词只包含小写英文字母a-z

示例1:
输入:[" abcd ", " bnrt ", " crmy ", " dtye "]
输出:true
说明:第一行和第一列都读“abcd”。第二行和第二列都读“bnrt”。第三行和第三行专栏都是“crmy”。第四行和第四列都读“dtye”。
因此,它是一个有效的字平方。
示例2:
输入:[" abcd ", " bnrt ", " crm ", " dt "]
输出:true
说明:第一行和第一列都读“abcd”。第二行和第二列都读“bnrt”。第三行和第三行专栏都是“crm”。第四行和第四列都读“dt”。
因此,它是一个有效的字平方。
示例3:
输入:[" ball ", " area ", " read ", " lady "]
输出:false
说明:第三行读取“read”,而第三列读取“lead”。因此,它不是一个有效的字平方。

思路

这道题主要考察是对两个边界条件的处理.一种情况是[" abcdc", " bnrt ", " crm ", " dt "].一种情况是[" abcd", " bnrt", " crm", " dtc"]

只要依次比较不同层次上的字符就可以了.

代码

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

相关文章

  • 422-有效的单词方块

    有效的单词方块 题目 Given a sequence of words, check whether it fo...

  • 英语打卡day15

    新的单词 atomic 原子的 pane 长方块 bristle 短而硬的毛发 desposal 丢掉 vice ...

  • python每日一题总结8

    20180625 qzd 每日一题26 -- 有效单词词广场 给定一个单词序列,检查它是否构成一个有效单词广场。一...

  • 快速记单词方法,让一天记100+单词不是梦

    在哔站学到一个行之有效的背单词方法,亲身检测,非常有效,废话不说,直接上干货: 1、工具A4纸,笔,单词本(背单词...

  • 有效背单词

    背单词新发现,其实单词和汉语一样也有偏旁部首。 (1)背单词的时候寻找单词熟知的部分,然后换偏旁进行联想记忆,举个...

  • 有效记单词

    大量有效的记单词,你需要一种疯狂的态度,也需要行之有效的方法。今天的十个记忆单词的高效方法是无数英语达人经验的总结...

  • 研讨主题:阅读教学中的词汇处理

    现实中学生学习英语最大拦路虎就是单词。单词又是组成英语句子、段落、篇章的“砖瓦”。如何处理好单词,如何有效处理单词...

  • 如何有效的背单词

    转眼间到了大三快要结束了。英语四级考了三次了,每次都220多分。成绩很稳定,但离四级线还有200多分。学校规定如果...

  • 小学生如何正确的提高英语成绩

    单词短语:天天默写 天天检查单词,短语的默写的情况。也方便回头看看孩子自己哪些单词短语容易写错。这个方法简单有效。...

  • 如何正确有效地背单词?

    开学了,你背了多少单词了?还是学习时间不断被压榨,最后全贡献给了手机? 到底如何有效背单词呢? 1.掌握自己最有效...

网友评论

      本文标题:422-有效的单词方块

      本文链接:https://www.haomeiwen.com/subject/ejbzjctx.html