Highly Readable Code in Java


  • 9

    Code readability is also an important aspect when we are doing interviews. Split it into two functions.

    public class Solution {
        public boolean validWordSquare(List<String> words) {
            if(words.size() == 0) return true;
            
            for(int i=0; i<words.size(); i++){
                String s = words.get(i);
                if(!s.equals(getVerticalString(i, words))){
                    return false;
                }
            }
            
            return true;
        }
        
        String getVerticalString(int col, List<String> words){
            StringBuilder sb = new StringBuilder();
            
            for(int i=0; i<words.size(); i++){
                String word = words.get(i);
                if(col < word.length()){
                    sb.append(word.charAt(col));
                }
                
            }
            
            return sb.toString();
        }
    }
    

  • 0
    L

    Very clean and nice. Props!


Log in to reply
 

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