Given a sequence of words, check whether it forms a valid word square.
A sequence of words forms a valid word square if thekthrow 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 alphabeta-z.
Example 1:
Input:[? "abcd",? "bnrt",? "crm",? "dt"] ??Output:true
Example 2:
Input:[? "ball",? "area",? "read",? "lady"]?Output:false
雙重循環(huán) 橫縱判斷遇到不等立即return false;
public boolean validWordSquare(Listwords) {
? ? 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;
}