Java 10 line naive solution


  • 0
    public boolean validWordSquare(List<String> words) {
        if(words.size()==0) return true;
        int m = words.size();
        for(int i = 0;i<m;i++){
            int n = words.get(i).length();
            if(n<words.size()&&words.get(n).length()>i) return false; 
            for(int j = 0;j<n;j++){
                if(words.size()<=j||words.get(j).length()<=i||words.get(j).charAt(i)!=words.get(i).charAt(j)) return false;
            }
        }
        return true;
    }

Log in to reply
 

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