Java Solution


  • 0

    The pattern is:
    row 0 & col 0 = row 0 & col 0
    row 0 & col 1 = row 1 & col 0
    row 0 & col 2 = row 2 & col 0
    ....
    So, just swap the row and column values.

    public class Solution {
        public boolean validWordSquare(List<String> words) {
            try{
                for(int row = 0; row <words.size();row++){
                    String word = words.get(row);
                    for(int col = 0;col<word.length();col++){
                        String word_cmp = words.get(col);
                        if(word.charAt(col) != word_cmp.charAt(row)){
                            return false;
                        }
                    }
                }
            }catch(StringIndexOutOfBoundsException e){
                return false;
            }catch(IndexOutOfBoundsException ee){
                return false;
            }
            return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.