
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Print all permutations in sorted (lexicographic) order in C++
- Finding power set for a set in JavaScript 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
- Power Set
- Print a number as string of 'A' and 'B' in lexicographic order in C++
- Select with set order in MySQL
- Program to generate first n lexicographic numbers in python
- Get the returned record set order in MySQL IN clause?
- Order by date set with varchar type in MySQL
- Integrate a polynomial and set the order in Python
- 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?
- Power in AC Circuit – Active Power, Reactive Power, Apparent Power
