Fast java solution


  • 0
    M
    public boolean validWordSquare(List<String> words) {
        int[] colLen = new int[words.size()];
        int prev = words.size();
        for(String word: words) {
            if(word.length() > prev) return false;
            prev = word.length();
            for(int i = 0; i < prev; i++)
                colLen[i]++;
        }
        for(int i = 0; i < words.size(); i++) {
            if(colLen[i] != words.get(i).length())
                return false;
        }
        for(int i = 0; i < words.size() - 1; i++) {
            String word = words.get(i);
            for(int j = i; j < word.length(); j++) {
                if(word.charAt(j) != words.get(j).charAt(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.