Easy Java Solution by converting List<String> to char[][]


  • 0
    public boolean validWordSquare(List<String> words) {
        int col = Integer.MIN_VALUE;
        for(String word : words)
            col = word.length() > col ? word.length() : col;
        if(words.size() != col)
            return false;
        char[][] arr = new char[words.size()][col];
        for(int i=0; i<words.size(); i++) 
            for(int j=0; j<col; j++) 
                arr[i][j] = j<words.get(i).length() ? words.get(i).charAt(j) : '1';
        for(int i=0; i<arr.length; i++)
            for(int j=0; j<col; j++)
                if(arr[i][j] != arr[j][i])
                    return false;
        return true;
    }

Log in to reply
 

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