In some problems like permutations, we fill positions one by one, while in others like subsets or partitioning, we move ahead based on the elements we have already chosen. Why do these problems move differently after each step?
.For returning all possible permutations:
class Solution {
public List> permute(int[] nums) {
List> ans=new ArrayList<>();
set(0,nums,ans);
return ans;
}
public void set(int ind,int nums[], List> ans)
{
if(ind==nums.length)
{
return; //logic to return ans
}
for(int i=ind;i
For returning palindrome partitioning of string:
class Solution {
public List> partition(String s) {
List> ans=new ArrayList<>();
List ds=new ArrayList<>();
partition(0,ans,ds,s);
return ans;
}
public void partition(int ind, List> ans,List ds,String s)
{
if(ind==s.length())
{
return; //logic to return ans
}
for(int i=ind;i
Now can u explain why did i use i+1 to call recursion inside for loop in permutations.. and why did I use ind+1 to call recursion inside for loop in palindrome partitioning.