Test 30 fails, expected 11 output 16


  • 0
    P

    Hi all, Does anyone have an insight why this code fails
    https://leetcode.com/submissions/detail/91524994/

    In case the submission is not accessible the code is

     public long ladderLength(String beginWord, String endWord, List<String> wordList,
          Map<String, Long> cache) {
        if (cache.containsKey(beginWord + endWord))
          return cache.get(beginWord + endWord);
    
        if (!wordList.contains(endWord))
          return Integer.MAX_VALUE;
        if (isTransformable(beginWord, endWord))
          return 2;
        if (beginWord.equals(endWord))
          return 1;
    
        long min = Integer.MAX_VALUE;
        for (String word : wordList) {
          if (isTransformable(beginWord, word)) {
    
            List<String> copy = new ArrayList<String>(wordList);
            copy.remove(word);
            copy.remove(beginWord);
            min = Math.min(min, 1 + ladderLength(word, endWord, copy, cache));
            cache.put(beginWord + endWord, min);
    
          }
        }
    
        return min;
      }
    
      public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        long min = ladderLength(beginWord, endWord, wordList, new HashMap<>());
        return (int) (min == Integer.MAX_VALUE ? 0 : min);
      }
    
      private static boolean isTransformable(String a, String b) {
    
        int diff = 0;
        for (int i = 0; i < a.length(); i++) {
          if (a.charAt(i) != b.charAt(i))
            diff++;
        }
        return diff == 1;
      }
    

Log in to reply
 

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