LeetCode:判断其是否形成了一个有效的单词方块

该篇博客探讨了一个编程问题,即判断给定的单词序列是否形成有效的单词方块。有效条件为每一行和每一列读取的字符串都相同。博客提供了Java和C++两种语言的解决方案,通过检查对角线上的字符对称性和行列相等性来确定有效性。示例展示了如何处理不同大小的输入并返回正确的判断结果。
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".

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".
题目大意:
给你一个单词序列,判断其是否形成了一个有效的单词方块.
有效的单词方块是指此由单词序列组成的文字方块的第k行和第k列(0≤k<max(行数,列数))所显示的字符串完全相同.

解题思路:
方法一:
行列相等则当个字符在沿着对角线上是对称的.
显而易见,这道题分别遍历ans[n][i]与ans[i][n]是否相等.需要注意的时,当字符串的字符个数小于数组的个数时,
进行加空格.

Java语言实现

package validWordSquare;

import java.util.*;

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

    public static void main(String[] args){
        String[] w={"abcd","bnrt","crmy","dtye"};
        List<String> words=new ArrayList<>();
        for(String s:w){
            words.add(s);
        }
        //List<String> words=Arrays.asList(w);
        System.out.println(new Solution().validWordSquare(words));
    }
}

C++语言实现

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Solution{
public:
    bool validWordSquare(vector<string>& words){
        int n=words.size();
        vector<vector<char>> ans(n,vector<char>(n,' '));
        for(int i=0;i<n;i++){
            if(words[i].size()>n)  return false;
            if(words[i].size()<n){
                words[i]+=string(n-words[i].size(),' ');//补充空格字符
            }
        }
        bool flag=true;
        for(int k=0;k<n;k++){
            for(int i=k+1;i<n;i++){
                 if(words[k][i]!=words[i][k]){
                    flag=false;
                    break;
                }
            }
        }
        return flag;
    }
};


int main(int argc,char* argv[]){
    vector<string> words={"abcd","bnrt","crmy","dtye"};
    cout<<Solution().validWordSquare(words)<<endl;
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值