Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions Leetcode_282.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//Way1
//Generate all possible numbers
//Apply all possible combinations
//Once last index is reached, check whether target is made or not
//If equals to target, add to result
//TC: O(4ⁿ * n
//SC: n^2
class Solution {
List<String> result;
public List<String> addOperators(String num, int target) {
this.result=new ArrayList<>();
helper(num,0,0,0,"",target);
return result;

}

private void helper(String num, int pivot, long calc, long prev, String path, int target){
//
if(pivot==num.length()){
if(calc==target){
result.add(path);
return;
}
}



for(int i=pivot;i<num.length();i++){
if(num.charAt(pivot)=='0' && i!=pivot) break;
long curr=Long.parseLong(num.substring(pivot,i+1));
if(pivot==0){
helper(num,i+1,curr,curr,path + curr,target);
}else{
//+
helper(num,i+1,calc+curr,curr,path + '+' + curr,target);


//-
helper(num,i+1,calc-curr,-curr,path + '-' + curr,target);


//*
helper(num,i+1,calc-prev+prev*curr,curr*prev,path + '*' + curr,target);
}
}
}
}


//way2
//Same prev approach - with string builder
//Generate all possible numbers
//Apply all possible combinations
//Once last index is reached, check whether target is made or not
//If equals to target, add to result
//TC: O(4^n)
//SC: n
class Solution {
List<String> result;
public List<String> addOperators(String num, int target) {
this.result=new ArrayList<>();
helper(num,0,0,0,new StringBuilder(),target);
return result;

}

private void helper(String num, int pivot, long calc, long prev, StringBuilder path, int target){
//
if(pivot==num.length()){
if(calc==target){
result.add(path.toString());
return;
}
}



for(int i=pivot;i<num.length();i++){
if(num.charAt(pivot)=='0' && i!=pivot) break;
long curr=Long.parseLong(num.substring(pivot,i+1));
if(pivot==0){
int len=path.length();
path.append(curr);
helper(num,i+1,curr,curr,path,target);
path.setLength(len);
}else{
//+
int len=path.length();
path.append('+');
path.append(curr);
helper(num,i+1,calc+curr,curr,path,target);
path.setLength(len);


//-
len=path.length();
path.append('-');
path.append(curr);
helper(num,i+1,calc-curr,-curr,path,target);
path.setLength(len);


//*
len=path.length();
path.append('*');
path.append(curr);
helper(num,i+1,calc-prev+prev*curr,curr*prev,path,target);
path.setLength(len);
}
}
}
}
71 changes: 71 additions & 0 deletions Leetcode_39.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//Way1
//DFS - With backtracking
//Choose case - Same element can be choosen again - So, pass i as i only
//No choose case - element is not selected - So, move to next element. i.e., i+1
//Base case - check if sum==0. If yes, add to result
//And every step, back track the path to regain the orifinal state
//TC: 2^(m+n)
//SC: m+n
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> res=new ArrayList<>();
dfs(candidates,0,target,new ArrayList<>(),res);
return res;

}
private void dfs(int[] candidates, int i, int target, List<Integer> path, List<List<Integer>> result){
//base case
if(target<0 || i==candidates.length){
return;
}

if(target==0){
result.add(new ArrayList<>(path));
return;
}


//actual logic
//no choose
dfs(candidates,i+1,target,path,result);

//choose
path.add(candidates[i]);
dfs(candidates,i,target-candidates[i],path,result);
//backtrack
path.remove(path.size()-1);
}
}


//way2
//for loop based recurssion - with backtracking
//in for loop - start with i=0
//call with same i=0 till target becomes to negative
//Once target = 0, add the path to result
//And when iteration returned, new one will start with i+1
//process goes on
//TC: O(n * 2^(m+n)) where m is the candidates length and n is the target
//SC: O(n * h) -> O(n^2)
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> ans = new ArrayList<>();
helper(candidates,0,target,new ArrayList<>(),ans);
return ans;

}
private void helper(int[] candidates, int pivot, int target,List<Integer> path, List<List<Integer>> ans){
if(target<0){
return;
}
if(target==0){
ans.add(new ArrayList<>(path));
}

for(int i=pivot;i<candidates.length;i++){
path.add(candidates[i]);
helper(candidates,i,target - candidates[i],path,ans);
path.remove(path.size()-1);
}
}
}