Problem

Approach

Time Complexity
Auxiliary Space

My Code

 

Code Answer

 
// Javascript program to generate power set
	
	// str : Stores input string
	// curr : Stores current subset
	// index : Index in current subset, curr
	function powerSet(str,index,curr)
	{
		let n = str.length;
 
		// base case
		if (index == n)
		{
			return;
		}
 
		// First print current subset
		document.write(curr+"<br>");
 
		// Try appending remaining characters
		// to current subset
		for (let i = index + 1; i < n; i++)
		{
			curr += str[i];
			powerSet(str, i, curr);
 
			// Once all subsets beginning with
			// initial "curr" are printed, remove
			// last character to consider a different
			// prefix of subsets.
			curr = curr.substring(0, curr.length - 1);
		}
	}
	
	// Driver code
	let str = "abc";
	let index = -1;
	let curr = "";
	powerSet(str, index, curr);
	
/**
a
ab
abc
ac
b
bc
c
**/

Python

# Python3 program to generate power set
def powerSet(string, index, curr):
 
	# string : Stores input string
	# curr : Stores current subset
	# index : Index in current subset, curr
	if index == len(string):
		print(curr)
		return
 
	powerSet(string, index + 1,
			curr + string[index])
	powerSet(string, index + 1, curr)
 
 
# Driver Code
if __name__ == "__main__":
 
	s1 = "abc"
	index = 0
	curr = ""
	powerSet(s1, index, curr)
 
# This code is contributed by Ekta Singh