Java neat and concise solution.


  • 0
    P
    public boolean validWordSquare(List<String> words) {
            
            for(int i = 0 ; i < words.size(); i++) {
                String str = words.get(i);
                for (int j = 0; j < words.size() && j < str.length();j++) {
                    if(words.get(j).length() <= i || str.length() > words.size())
                        return false;
                    if(str.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.