

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Power Set in Lexicographic order in C++
In this problem, we are given string str. Our task is to print the power set of this string’s elements in lexicographical order.
Power Set − The power set of a set is the set of all subsets of the set. Denoted by P(S) where s is the set.
Example −
S = {1, 2, 3} ; p(S) = {{}, {1}, {1, 2}, {1, 3}, {2}, {2, 3}, {3}, {1,2,3}}
In this problem, we will treat the string as a set. So, its characters will be the elements of the set.
Let’s take an example to understand the problem
Input − str = “xyz”
Output − x xy xyz xz y yz z
To solve this problem, we will have to sort the array, so that lexicographical order can be obtained. Then we will fix one element of the string and recursively call for the rest elements which will generate all the substring. And discard the first fixed element to obtain the next permutation.
Example
Program to show the implementation of our solution,
#include <bits/stdc++.h> using namespace std; void printAllSubsets(string str, int n, int index = -1, string subset = "") { if (index == n) return; cout<<subset<<"\n"; for (int i = index + 1; i < n; i++) { subset+= str[i]; printAllSubsets(str, n, i, subset); subset = subset.erase(subset.size() - 1); } return; } void GeneratePowerSet(string str) { sort(str.begin(), str.end()); printAllSubsets(str, str.size()); } int main() { string str = "xyz"; cout<<"Power Set of the string '"<<str<<"' is :\n"; GeneratePowerSet(str); return 0; }
Output
Power Set of the string 'xyz' is: x xy xyz xz y yz z
- Related Questions & Answers
- Print all permutations in sorted (lexicographic) order in C++
- Finding power set for a set in JavaScript Power Set
- Power Set
- Find a string in lexicographic order which is in between given two strings in Python
- Python Program to Print All Permutations of a String in Lexicographic Order without Recursion
- Python Program to Print All Permutations of a String in Lexicographic Order using Recursion
- Select with set order in MySQL
- Print a number as string of 'A' and 'B' in lexicographic order in C++
- Program to generate first n lexicographic numbers in python
- Order by date set with varchar type in MySQL
- Integrate a polynomial and set the order in Python
- Get the returned record set order in MySQL IN clause?
- Power Triangle and Power Factor in AC Circuits
- MySQL query to first set negative value in descending order and then positive value in ascending order
- How to set the tab order in a Tkinter application?