From f82797dd802ba6d58deefcf40c9efd23aa46468a Mon Sep 17 00:00:00 2001 From: Richa Patil Date: Wed, 13 May 2026 23:44:12 +0530 Subject: [PATCH 1/2] BackTracking Combination Sum Solution --- 39. Combination Sum/Solution.class | Bin 0 -> 1317 bytes 39. Combination Sum/Solution.java | 40 +++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 39. Combination Sum/Solution.class create mode 100644 39. Combination Sum/Solution.java diff --git a/39. Combination Sum/Solution.class b/39. Combination Sum/Solution.class new file mode 100644 index 0000000000000000000000000000000000000000..208919fec77e67e7ca3c9afcc6cd20a113bc3689 GIT binary patch literal 1317 zcma)6OK%ck6g?lzFojNEwu6Nhsn7BfeAX&0TB}A9Dkc~VcA*1|ICYq5rbCmu_E+e} zxN&Q{utiO`CjKO2)cXxImCC{e@V)Li_ndn#-+z7H2e5>@0eIk*AjlBmV;EgiH`R=x znvG1cx~A0}2H%2i>Q0Wq8;h3%7=T|wK*k_shJli0v>e?s8HQ_Cv#Oga-<4WT1|_zd z&&LZcAiXg%1%1QG`Z0_V38ON`aIA-~w6)=A6z8>C#Hw@xbu07G)=j76*qYkR4q^g| zgrJP$m?X|#aLUKA=%sEp7$#y}ZdXj_G#fw|Qxc|SL~w#(+>P($V35{1qd}UKSSO3W zA>w5TGY6rrt|i-6x45_fqVRHuPRlrhvt->1%~;oLhS^wtHGkBKe-604QOxpE#27+d za+hP-G7PE)d7F)wxeEyyNu%zA$Vj*#<039Gj2+fCZ#r6oCnjyG zMoTL`X9&g)ish>9zHlACBI7FN7=#V|mFD(Ao~JM`<2r7TN?U7Mn{=V{LUi+>xMizO z*CPep)E=~&Rn2~+Rt-84nyOAV2V<-Kd}i>M^agcQ%ci#r{n}~ah^O6skAhq_mt`0# zIcn|Yin`vVuJx(N|Dqi&Tz}ShnITZJT6Rslr*pMK2g8!)g}@9oiid^}5a>hG2Zk)I zBJ|7nNn#gln--* zHy+#~H0t>-+K#IScou$xi4=zk3A;GgM%;tP+u^tHN0R&RAZ$nGL`6*QzsGz?NGf7_ z59d3N!hBaxrW7$G5PwJr32j`amZ`UKjX++D?8EPGT${(WCrw&F(=A|%T5}o-O;VIR qo}ru)n59mQ<33WT(Eceh^w$T{E-HTfgophg4>~$-lgecm_0At|6DZdJ literal 0 HcmV?d00001 diff --git a/39. Combination Sum/Solution.java b/39. Combination Sum/Solution.java new file mode 100644 index 00000000..09fdc287 --- /dev/null +++ b/39. Combination Sum/Solution.java @@ -0,0 +1,40 @@ +class Solution { + public List> combinationSum(int[] candidates, int target) { + + List> result = new ArrayList<>(); + + helper(target, candidates, result, 0, new ArrayList<>()); + + return result; + + } + + public void helper(int target, int[] candidates, List> result, int pivot, List path){ + + //base case + if(target == 0){ + result.add(new ArrayList<>(path)); + return; + } + + if(target < 0 || pivot == candidates.length){ + return; + } + + + //for loop base recursion + for(int i = pivot; i < candidates.length; i++){ + + path.add(candidates[i]); + + helper(target - candidates[i], candidates, result, i, path); + + //backtrack + path.remove(path.size() - 1); + + } + } +} + +// TC - O(2^(m+n)) +// SC - O(n^2) \ No newline at end of file From 8a2b4da0db755c123a884121c058af42aa73cfb7 Mon Sep 17 00:00:00 2001 From: Richa Patil Date: Thu, 14 May 2026 23:31:44 +0530 Subject: [PATCH 2/2] Expression add Operators solution --- 282. Expression Add Operators/Solution.java | 64 ++++++++++++++++++++ 39. Combination Sum/Solution.class | Bin 1317 -> 0 bytes 2 files changed, 64 insertions(+) create mode 100644 282. Expression Add Operators/Solution.java delete mode 100644 39. Combination Sum/Solution.class diff --git a/282. Expression Add Operators/Solution.java b/282. Expression Add Operators/Solution.java new file mode 100644 index 00000000..24a61a88 --- /dev/null +++ b/282. Expression Add Operators/Solution.java @@ -0,0 +1,64 @@ +class Solution { + public List addOperators(String num, int target) { + + List result = new ArrayList(); + + helper(num, target, 0, 0, 0, new StringBuilder(), result); + + return result; + + } + + public void helper(String num, int target, long calc, long tail, int pivot, StringBuilder path, + List result) { + + //base case + if (pivot == num.length()) { + if (calc == target) { + result.add(path.toString()); + } + } + + //for loop + for (int i = pivot; i < num.length(); i++) { + + if (num.charAt(pivot) == '0' && i != pivot) { + break; + } + + long cur = Long.parseLong(num.substring(pivot, i + 1)); // substring take +1 address to strip + + int length = path.length(); + + if (pivot == 0) { + + path.append(cur); + + helper(num, target, cur, cur, i + 1, path, result); + + path.setLength(length); + + } else { + + //+ + path.append("+").append(cur); + helper(num, target, calc + cur, cur, i + 1, path, result); + path.setLength(length); + + //- + path.append("-").append(cur); + helper(num, target, calc - cur, -cur, i + 1, path, result); + path.setLength(length); + + //* + path.append("*").append(cur); + helper(num, target, calc - tail + tail * cur, tail * cur, i + 1, path, result); + path.setLength(length); + + } + } + } +} + +// TC - 4*n +// SC - O(n) \ No newline at end of file diff --git a/39. Combination Sum/Solution.class b/39. Combination Sum/Solution.class deleted file mode 100644 index 208919fec77e67e7ca3c9afcc6cd20a113bc3689..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1317 zcma)6OK%ck6g?lzFojNEwu6Nhsn7BfeAX&0TB}A9Dkc~VcA*1|ICYq5rbCmu_E+e} zxN&Q{utiO`CjKO2)cXxImCC{e@V)Li_ndn#-+z7H2e5>@0eIk*AjlBmV;EgiH`R=x znvG1cx~A0}2H%2i>Q0Wq8;h3%7=T|wK*k_shJli0v>e?s8HQ_Cv#Oga-<4WT1|_zd z&&LZcAiXg%1%1QG`Z0_V38ON`aIA-~w6)=A6z8>C#Hw@xbu07G)=j76*qYkR4q^g| zgrJP$m?X|#aLUKA=%sEp7$#y}ZdXj_G#fw|Qxc|SL~w#(+>P($V35{1qd}UKSSO3W zA>w5TGY6rrt|i-6x45_fqVRHuPRlrhvt->1%~;oLhS^wtHGkBKe-604QOxpE#27+d za+hP-G7PE)d7F)wxeEyyNu%zA$Vj*#<039Gj2+fCZ#r6oCnjyG zMoTL`X9&g)ish>9zHlACBI7FN7=#V|mFD(Ao~JM`<2r7TN?U7Mn{=V{LUi+>xMizO z*CPep)E=~&Rn2~+Rt-84nyOAV2V<-Kd}i>M^agcQ%ci#r{n}~ah^O6skAhq_mt`0# zIcn|Yin`vVuJx(N|Dqi&Tz}ShnITZJT6Rslr*pMK2g8!)g}@9oiid^}5a>hG2Zk)I zBJ|7nNn#gln--* zHy+#~H0t>-+K#IScou$xi4=zk3A;GgM%;tP+u^tHN0R&RAZ$nGL`6*QzsGz?NGf7_ z59d3N!hBaxrW7$G5PwJr32j`amZ`UKjX++D?8EPGT${(WCrw&F(=A|%T5}o-O;VIR qo}ru)n59mQ<33WT(Eceh^w$T{E-HTfgophg4>~$-lgecm_0At|6DZdJ