java 422.有效的Word Square(Solution1).java

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 422.有效的Word Square(Solution1).java相关的知识,希望对你有一定的参考价值。

import java.util.*;
public class Solution {
    public boolean validWordSquare(List<String> words) {
        if (words == null) return false;
        int row = words.size();
        int col = words.get(0).length();
        if (row != col) return false;
        for (int i = 1; i < row; i++) {
            if (words.get(i).length() > row) return false; //["abc","bde","cefg"]
            StringBuilder sb = new StringBuilder();
            for (int j = 0; j < i; j++) {
                String str = words.get(j);
                if (i < str.length()) {
                    sb.append(words.get(j).charAt(i));
                } else {
                	sb.append('#'); // ["ball","asee","let","lep"]
                }
            }
            String prefix = sb.toString();
            StringBuilder word = new StringBuilder(words.get(i));
            while(word.length() < prefix.length()) {
            	word.append("#");
            }
            
            if (!word.toString().startsWith(prefix)) return false;
        }
        return true;
    }
}
public class Solution {
    public boolean validWordSquare(List<String> words) {
        if(words == null || words.size() == 0){
            return true;
        }
        int n = words.size();
        for(int i=0; i<n; i++){
            for(int j=0; j<words.get(i).length(); j++){
                if(j >= n || words.get(j).length() <= i || words.get(j).charAt(i) != words.get(i).charAt(j))
                    return false;
            }
        }
        return true;
    }
}

以上是关于java 422.有效的Word Square(Solution1).java的主要内容,如果未能解决你的问题,请参考以下文章

java 422.有效的Word Square(Solution1).java

java 422.有效的Word Square(Solution1).java

java 422.有效的Word Square(Solution1).java

java 422.有效的Word Square(Solution1).java

422. Valid Word Square

422. Valid Word Square