package com.fishercoder.solutions;

import java.util.ArrayList;
import java.util.List;

/**
 * Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value.

 Examples:
 "123", 6 -> ["1+2+3", "1*2*3"]
 "232", 8 -> ["2*3+2", "2+3*2"]
 "105", 5 -> ["1*0+5","10-5"]
 "00", 0 -> ["0+0", "0-0", "0*0"]
 "3456237490", 9191 -> []
 */
public class _282 {

    public List<String> addOperators(String num, int target) {
        List<String> res = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        dfs(res, sb, num, 0, target, 0, 0);
        return res;

    }

    private void dfs(List<String> res, StringBuilder sb, String num, int pos, int target, long prev, long multi) {
        if (pos == num.length()) {
            if (target == prev) {
                res.add(sb.toString());
            }
            return;
        }
        for (int i = pos; i < num.length(); i++) {
            if (num.charAt(pos) == '0' && i != pos) {
                break;
            }
            long curr = Long.parseLong(num.substring(pos, i + 1));
            int len = sb.length();
            if (pos == 0) {
                dfs(res, sb.append(curr), num, i + 1, target, curr, curr);
                sb.setLength(len);
            } else {
                dfs(res, sb.append("+").append(curr), num, i + 1, target, prev + curr, curr);
                sb.setLength(len);

                dfs(res, sb.append("-").append(curr), num, i + 1, target, prev - curr, -curr);
                sb.setLength(len);

                dfs(res, sb.append("*").append(curr), num, i + 1, target, prev - multi + multi * curr, multi * curr);
                sb.setLength(len);
            }
        }
    }

}

@@ -85,16 +85,52 @@
 t;%0A%0A/**%0A
+ * 282. Expression Add Operators%0A *%0A
  * Given
@@ -188,16 +188,19 @@
 t value,
+%0A *
  return 
@@ -282,16 +282,19 @@
 e digits
+%0A *
  so they
@@ -515,16 +515,56 @@
 _282 %7B%0A%0A
+    public static class Solution1 %7B%0A    
     publ
@@ -626,16 +626,20 @@
         
+    
 List%3CStr
@@ -682,16 +682,20 @@
         
+ 
+   
 StringBu
@@ -726,32 +726,36 @@
 lder();%0A        
+   
+ 
 dfs(res, sb, num
@@ -778,24 +778,28 @@
 0);%0A        
+    
 return res;%0A
@@ -803,19 +803,27 @@
 s;%0A%0A    
-%7D%0A%0A
+    %7D%0A%0A    
     priv
@@ -929,32 +929,36 @@
 ulti) %7B%0A        
+    
 if (pos == num.l
@@ -976,24 +976,28 @@
             
+   
+ 
 if (target =
@@ -1002,24 +1002,28 @@
  == prev) %7B%0A
+    
             
@@ -1054,34 +1054,42 @@
 ));%0A            
+  
-%7D%0A
+  %7D%0A    
             retu
@@ -1100,26 +1100,33 @@
         
-%7D%0A
+    %7D%0A   
         
 for (int
@@ -1117,16 +1117,17 @@
         
+ 
 for (int
@@ -1157,24 +1157,28 @@
 h(); i++) %7B%0A
+    
             
@@ -1239,15 +1239,23 @@
     
+    
 break;%0A
+    
     
@@ -1276,16 +1276,20 @@
         
+    
 long cur
@@ -1347,16 +1347,20 @@
         
+    
 int len 
@@ -1378,32 +1378,36 @@
 ();%0A            
+    
 if (pos == 0) %7B%0A
@@ -1414,32 +1414,36 @@
                 
+ 
+   
 dfs(res, sb.appe
@@ -1493,32 +1493,36 @@
                 
+    
 sb.setLength(len
@@ -1536,16 +1536,20 @@
         
+    
 %7D else %7B
@@ -1557,32 +1557,36 @@
                 
+   
+ 
 dfs(res, sb.appe
@@ -1655,32 +1655,36 @@
                 
+    
 sb.setLength(len
@@ -1695,32 +1695,36 @@
                 
+  
+  
 dfs(res, sb.appe
@@ -1782,24 +1782,28 @@
 rr, -curr);%0A
+    
             
@@ -1834,32 +1834,36 @@
                 
+    
 dfs(res, sb.appe
@@ -1956,32 +1956,36 @@
                 
+    
 sb.setLength(len
@@ -1979,32 +1979,50 @@
 setLength(len);%0A
+                %7D%0A
             %7D%0A  

refactor 282