Java


  • 0
    J

    Hashmap usage

    public boolean validWordSquare(List<String> words) {
        Map map = new HashMap<Integer, String>();
        Map rotateMap = new HashMap<Integer, String>();
        for (int i=0; i<words.size(); i++) {
            String word = words.get(i);
            map.put(i, word);
    
            for (int j=0; j<word.length(); j++) {
                String value = (String)rotateMap.getOrDefault(j, "");
                value += String.valueOf(word.charAt(j));
                rotateMap.put(j, value);
            }
        }
    
        for (Object key : map.keySet()) {
            if (!map.get(key).equals(rotateMap.get(key))) {
                return false;
            }
        }
        return true;

Log in to reply
 

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