Power Set | Practice | GeeksforGeeks

Given a string S find all possible subsequences of the string in lexicographically-sorted order.
Example 1:
Input : str = "abc"
Output: a ab abc ac b bc c
Explanation : There are 7 substrings that 
can be formed from abc.

Example 2:
Input: str = "aa"
Output: a a aa
Explanation : There are 3 substrings that 
can be formed from aa.

Your Task:
You don't need to read ot print anything. Your task is to complete the function AllPossibleStrings() which takes S as input parameter and returns a list of all possible substrings(non-empty) that can be formed from S in lexicographically-sorted order.
 
Expected Time Complexity: O(2n) where n is the length of the string
Expected Space Complexity : O(n * 2n)
 
Constraints: 
1 <= Length of string <= 16
+1
60mqcs20shivamyadav1 day ago
#Python solution
def AllPossibleStrings(self, s): # Code here a= '' lis =[] power_set = int(math.pow(2,len(s))) for count in range(1,power_set):     for j in range(0,len(s)):         if count & (1 <<j) > 0:             if s[j] !=" ":                 a = a + s[j]     lis.append(a)     a = '' lis.sort() return lis
0
mitradiptamoy5 days ago
//C++ solution   O(2^n+n+nlog n)
 
vector<string> AllPossibleStrings(string s){     // Code here     int n=s.length();     vector<string>ans;     for(int num=0;num<pow(2,n);num++)     {         string c="";         for(int j=0;j<n;j++){             if(num & (1<<j)){                 c+=s[j];             }         }         if(c!=""){             ans.push_back(c);         }     }     sort(ans.begin(),ans.end());     return ans; }
0
anuragkr8981 week ago
// Java Code
class Solution
{
    public List<String> AllPossibleStrings(String s)
    {
        int n = s.length();
        List<String> ans = new ArrayList<>();
        for(int i=0; i< (1<<n); i++) {
            String k = "";
            for(int bit=0; bit<n; bit++) {
                if((i&(1<<bit)) > 0)
                    k += s.charAt(bit);
            }
            if(k != "")
                ans.add(k);
        }
        Collections.sort(ans);
        return ans;
        
    }
}
 
0
malimukesh1 week ago
I don't know why this JavaScript code is not getting accepted. Locally I tested it is returning expected result. Can someone check it ?
/**
 * @param {string} s
 * @return {string[]}
*/

class Solution {
    findPowerSet(S, i, curr, res) {
        if(i === S.length) {
            // console.log(curr);
            res.push(curr);
            return;
        }
        // consider current index
        this.findPowerSet(S, i+1, curr+S[i], res);
        this.findPowerSet(S, i+1, curr, res);
        res.splice(res.indexOf(""), res.indexOf("") + 1);
        return res;
    }
    AllPossibleStrings(s){
        //code here
        let res = this.findPowerSet(s, 0, "", []);
        res.sort( (a,b) => a >b);
        return  res;
    }
}
0
mankesh0161 week ago
vector<string> AllPossibleStrings(string s ) {
		    
		    vector<string> ans ;
 
		    int n= s.length() ;
 
		    for(int i=1; i < (1<<n); i++){
		        string str="";
 
		        for(int j=0; j<n; j++){
		            if(i & 1<<j) str+=s[j];
		        }
		        ans.push_back(str);
		    }
		    sort(ans.begin(), ans.end());
		    return ans ;
		}	
+1
kashyapjhon2 weeks ago
C++ Solution Time=(0.29/1.6) EASY:
void help(vector<string> &ans,string s,string out){        if(s.size()==0){            ans.push_back(out);            return;        }        string out1=out;        string out2=out;        out2.push_back(s[0]);        s.erase(s.begin()+0);        help(ans,s,out1);        help(ans,s,out2);        return;    } vector<string> AllPossibleStrings(string s){     // Code here     vector<string> ans;     string out;     help(ans,s,out);     sort(ans.begin(),ans.end());     ans.erase(ans.begin()+0);     return ans; }
0
harshit68rocking2 weeks ago
	    void filter(int i,string s, vector<string> &ans){
	        int j=0;
	        int n=i;
	        while(n>0){
	            if(n&1){
	                ans[i].push_back(s[j]);
	            }
	            j++;
	            n=n>>1;
	        }
	        
	    }
		vector<string> AllPossibleStrings(string s){
		    // Code here
		    vector<string> ans(pow(2,s.length()),"");
		    for(int i=0;i<(1<<s.length());i++){
		     filter(i,s,ans);   
		    }
		    sort(ans.begin(),ans.end());
		    vector<string> v;
		    for(int i=1;i<ans.size();i++){
		        v.push_back(ans[i]);
		    }
            
		    return v;
		}
0
swarnendumondal20082 weeks ago
string CorrespondingSubstring(string s, int n)
	    {
	        string ans="";
	        for(int i=0;n!=0;i++)
	        {
	            if(n&1)
	            {
	                ans.push_back(s[i]);
	            }
	            n = n>>1;
	        }
	        return ans;
	    }
		vector<string> AllPossibleStrings(string s)
		{
		    // Code here
		    int n = (1<<(s.length()))-1; // important
		    vector<string> vec;
		    for(int i=1;i<=n;i++)
		    {
		        vec.push_back(CorrespondingSubstring(s,i));
		    }
		    sort(vec.begin(), vec.end());
		    return vec;
		}
0
subhajitghosh19973 weeks ago
// JAVA // passed all the testcases 
public List<String> AllPossibleStrings(String s)   {       // Code here       List<String> ans = new ArrayList<>();       String in = s;       String out = "";       ans = generate(in , out , ans);       Collections.sort(ans);       ans.remove(0);       return ans;   }      public List<String> generate(String in , String out , List<String> ans){       if(in.length() == 0){           ans.add(out);           return ans;       }              char char_at_0 = in.charAt(0);       String left = in.substring(1);              generate(left , out , ans);       generate(left , out+char_at_0 , ans);       return ans;   }
0
ruchitchudasama1231 month ago
 public:vector<string> AllPossibleStrings(string s){    int n=s.length();    vector<string> ans;    for(int num=1;num<(1<<n);num++){        string sub="";        for(int i=0;i<n;i++){            if(num&(1<<i)){                sub+=s[i];            }        }        ans.push_back(sub);    }   // for(auto x:ans){   //     cout<<x<<endl;   // }   // cout<<"hello\n";   sort(ans.begin(),ans.end());    return ans;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.