12ms Solution


  • 0
    L
    public class Solution {
        public boolean validWordSquare(List<String> words) {
            int len = words.get(0).length();
            if (words.size() != len)
                return false;
            char[][] arr = new char[len][len];
            for (int i = 0; i < len; i++) {
                String cur = words.get(i);
                if (cur.length() > len)
                    return false;
                int j = 0;
                for (; j < cur.length(); j++) {
                    arr[i][j] = cur.charAt(j);
                }
                for (; j < len; j++)
                    arr[i][j] = '0';
            }
            for (int i = 1; i < len; i++) {
                for (int j = 0; j < i; 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.