Java AC Solution Easy to Understand


  • 23
    S

    Simply check for each row and columns, return false if not match.

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

  • 3
    J

    Here the input is a list - the code will work inefficiently if the list is a LinkedList.


  • 1
    C

    @star1993 Could you explain why use this condition "words.get(j).length() <= i "? Thanks!


  • 1
    B

    @cyanzhang You need to make sure words.get(j).charAt(i) is safe.


  • 1
    S

    @jeffery You are right, we can use List<String> word = new ArrayList<>(words);to convert it into an ArrayList.


  • 1
        public boolean validWordSquare(List<String> words) {
            
            for(int i=0;i<words.size();i++){
                char[] sc  = words.get(i).toCharArray();
                for(int j=0;j<sc.length;j++){
                    if(j >= words.size() || i >= words.get(j).length()) return false;
                    if(sc[j] != words.get(j).charAt(i)) return  false;
                }
            }        
            
            return true;
        }

  • 4
    I

    @cyanzhang said in Java AC Solution Easy to Understand:

    @star1993 Could you explain why use this condition "words.get(j).length() <= i "? Thanks!

    Think about the example of ["ball","asee","let","lep"], which looks like the following as a matrix:

    ball
    asee
    let
    lep
    

    when i is 3 and j is 2, words.get(i).charAt(j) will get p (at 4th row and 3th column). However, the words.get(j).charAt(i) statement will throw a java.lang.StringIndexOutOfBoundsException exception.


  • 1
    I

    @cyanzhang said in Java AC Solution Easy to Understand:

    @star1993 Could you explain why use this condition "words.get(j).length() <= i "? Thanks!

    Similarly, if (j >= n) return false is for input cases such as ["abc","bde","cefg"]


  • 0
    K

    @star1993

    You can use exception handling to avoid the need to check for conditions that would result in an ArrayIndexOutOfBoundsException or a StringIndexOutOfBoundsException. Ii think it makes the algorithm run marginally faster. Definitely not the kind of thing you would actually do on the job, though.

    public class Solution {
        public boolean validWordSquare(List<String> words) {
            try {
                for (int i = 0; i < words.size(); i ++) {
                    for (int j = 0; j < words.get(i).length(); j ++) {
                        if (words.get(i).charAt(j) != words.get(j).charAt(i)) {
                            return false;
                        }
                    }
                }
                
                return true;
            } catch (RuntimeException e) {
                return false;
            }
        }
    }
    

  • 1
    P

    @ivanlw How did you come up with this checking of ""words.get(j).length() <= i "? This condition checking actually covers a lot of scenarios. Is there a intrinsic way that you can describe/explain it so that it can be easily understood?


  • 0
    This post is deleted!

  • 0
    V

    @ivanlw Hi, I think the situation you talked about (when i=3 & j=2) won't happen. Because the function would return false before that. When i=2 and j=2, words.get(j).length() =2, which is equal to i. That means the function would return false.


  • 0
    Y
    This post is deleted!

  • 0
    F

    Do you really need the first if statement if we're guaranteed "1. The number of words given is at least 1 and does not exceed 500" and "2. Word length will be at least 1 and does not exceed 500"?


Log in to reply
 

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