package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/decode-ways/
 */
public class Problem91 {
    public int numDecodings(String s) {
        if (s.length() == 0) {
            return 0;
        }
        Map<Integer, Integer> memo = new HashMap<>();
        return decode(s, 0, memo);
    }

    private int decode(String s, int idx, Map<Integer, Integer> memo) {
        if (memo.containsKey(idx)) {
            return memo.get(idx);
        }
        if (idx == s.length()) {
            return 1;
        }
        int result = 0;
        if (idx + 1 <= s.length()) {
            String one = s.substring(idx, idx + 1);
            int num = Integer.parseInt(one);
            if (num >= 1 && num <= 9) {
                result += decode(s, idx + 1, memo);
            }
        }
        if (idx + 2 <= s.length()) {
            String two = s.substring(idx, idx + 2);
            int num = Integer.parseInt(two);
            if (num >= 10 && num <= 26) {
                result += decode(s, idx + 2, memo);
            }
        }
        memo.put(idx, result);
        return result;
    }
}

@@ -16,57 +16,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 /**%0A
@@ -143,115 +143,48 @@
     
-if (s.length() == 0) %7B%0A            return 0;%0A        %7D%0A        Map%3CInteger, Integer%3E memo = new HashMap%3C%3E
+Integer%5B%5D memo = new Integer%5Bs.length
 ()
+%5D
 ;%0A  
@@ -196,22 +196,28 @@
  return 
-decode
+numDecodings
 (s, 0, m
@@ -245,18 +245,31 @@
 ate 
-int decode
+static int numDecodings
 (Str
@@ -288,29 +288,17 @@
 dx, 
-Map%3C
 Integer
-, Integer%3E
+%5B%5D
  mem
@@ -318,28 +318,24 @@
 if (
-memo.containsKey(idx
+idx == s.length(
 )) %7B
@@ -358,21 +358,9 @@
 urn 
-memo.get(idx)
+1
 ;%0A  
@@ -379,33 +379,33 @@
     if (
-idx == s.length()
+memo%5Bidx%5D != null
 ) %7B%0A    
@@ -411,33 +411,41 @@
          return 
-1
+memo%5Bidx%5D
 ;%0A        %7D%0A    
@@ -452,22 +452,22 @@
     int 
-result
+answer
  = 0;%0A  
@@ -517,90 +517,59 @@
     
-String one = s.substring(idx, idx + 1);%0A            int num = Integer.parseInt(one
+int n1 = Integer.parseInt(s.substring(idx, idx + 1)
 );%0A 
@@ -587,23 +587,21 @@
 if (
-num %3E
+1 %3C
 = 
+n
 1 && n
-um
+1
  %3C= 
@@ -621,32 +621,38 @@
         
-result += decode
+answer += numDecodings
 (s, idx 
@@ -740,90 +740,59 @@
     
-String two = s.substring(idx, idx + 2);%0A            int num = Integer.parseInt(two
+int n2 = Integer.parseInt(s.substring(idx, idx + 2)
 );%0A 
@@ -810,24 +810,22 @@
 if (
-num %3E= 10
+10 %3C= n2
  && n
-um
+2
  %3C= 
@@ -850,24 +850,30 @@
     
-result += decode
+answer += numDecodings
 (s, 
@@ -928,25 +928,22 @@
 memo
-.put(idx, result)
+%5Bidx%5D = answer
 ;%0A  
@@ -959,14 +959,14 @@
 urn 
-result
+answer
 ;%0A  

Update problem 91