SarthakKeshari / Java-Questions-and-Solutions

This repository aims to solve and create new problems from different spheres of coding. A path to help students to get access to solutions and discuss their doubts.
MIT License
47 stars 98 forks source link

Power set #293

Closed 2devyank closed 3 years ago

2devyank commented 3 years ago

Enter your question -

Generate Power set using Bitwise operator Input="abc". output="","a","b","c","ac","ab","bc","abc".

Bit-manipulation

2devyank commented 3 years ago

@SarthakKeshari please assign me this issue

SarthakKeshari commented 3 years ago

@2devyank, Kindly elaborate in comments that how you are going to approach this problem.

2devyank commented 3 years ago

@SarthakKeshari

Question

Generate Power set using Bitwise operator

Sample Case

Input="ab" Output="","a","b","ab"

Approach

Basically what we have to do is check whether a bit is set or not if a bit is set then will print that character

So if n is the length of string given , then the number of subsets of that string is 2^n. Therefore ,will be running two loop firstly will run the outer loop from 0 to (2^n)-1 and inside will run loop of the length of string and will check using bitwise operator if a bit is set or not ,if bit is set then will print that character

SarthakKeshari commented 3 years ago

@SarthakKeshari

Question

Generate Power set using Bitwise operator

Sample Case

Input="ab" Output="","a","b","ab"

Approach

Basically what we have to do is check whether a bit is set or not if a bit is set then will print that character

So if n is the length of string given , then the number of subsets of that string is 2^n. Therefore ,will be running two loop firstly will run the outer loop from 0 to (2^n)-1 and inside will run loop of the length of string and will check using bitwise operator if a bit is set or not ,if bit is set then will print that character

@2devyank, Good to see a different approach. I appreciate your thought out of the box against the general trend of just using string handling for every string question.