Snippets Collections
class LexSort
{
    static void solve(String s,String out, ArrayList<String> ans ,int i){
        if(i==s.length()){
            ans.add(out);
            return;
        }
        char ch=s.charAt(i);
        solve(s,out,ans,i+1);
        solve(s,out+String.valueOf(ch),ans,i+1);
    }
    
    //Function to return the lexicographically sorted power-set of the string.
    static ArrayList<String> powerSet(String s)
    {
        ArrayList<String> ans= new ArrayList<>();
        solve(s,"",ans,0);
        return ans;
    }
}
star

Sun Feb 06 2022 23:27:01 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/power-set-using-recursion/1/?track=DSASP-Recursion&batchId=190

#java #gfg #geeksforgeeks #recursion #powerset

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension