Open In App

Generating distinct subsequences of a given string in lexicographic order

Last Updated : 10 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a string s, make a list of all possible combinations of letters of a given string S. If there are two strings with the same set of characters, print the lexicographically smallest arrangement of the two strings
For string abc, the list in lexicographic order subsequences are, a ab abc ac b bc c

Examples: 

Input : s = "ab"
Output : a ab b

Input  : xyzx
Output : x xx xy xyx xyz xyzx xz xzx y
         yx yz yzx z zx

The idea is to use a set (which is implemented using self balancing BST) to store subsequences so that duplicates can be tested.
To generate all subsequences, we one by one remove every character and recur for remaining string. 

Implementation:

C++





Java





Python 3




# Python program to print all distinct
# subsequences of a string.
 
# Finds and stores result in st for a given
# string s.
def generate(st, s):
    if len(s) == 0:
        return
 
    # If current string is not already present.
    if s not in st:
        st.add(s)
 
        # Traverse current string, one by one
        # remove every character and recur.
        for i in range(len(s)):
            t = list(s).copy()
            t.remove(s[i])
            t = ''.join(t)
            generate(st, t)
 
    return
 
 
# Driver Code
if __name__ == "__main__":
    s = "xyz"
    st = set()
    generate(st, s)
    for i in st:
        print(i)
 
# This code is contributed by
# sanjeev2552


C#




// C# program to print all distinct subsequences
// of a string.
using System;
using System.Collections.Generic;
 
class GFG {
 
    // Finds and stores result in st for a given
    // string s.
    static void generate(HashSet<String> st, String s)
    {
        if (s.Length == 0) {
            return;
        }
 
        // If current string is not already present.
        if (!st.Contains(s)) {
            st.Add(s);
 
            // Traverse current string, one by one
            // remove every character and recur.
            for (int i = 0; i < s.Length; i++) {
                String t = s;
                t = t.Substring(0, i) + t.Substring(i + 1);
                generate(st, t);
            }
        }
        return;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        String s = "xyz";
        HashSet<String> st = new HashSet<String>();
        generate(st, s);
        foreach(String str in st)
        {
            Console.WriteLine(str);
        }
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript




<script>
 
// JavaScript program to print
// all distinct subsequences
// of a string.
 
// Finds and stores result in st for a given
// string s.
function generate(st,s){
    if (s.length == 0)
        return st;
 
    // If current string is not already present.
    if (!st.has(s)) {
        st.add(s);
        // Traverse current string, one by one
        // remove every character and recur.
        for (let i = 0; i < s.length; i++) {
            let t = s;
            t = t.substr(0, i) + t.substr(i + 1);
            st = generate(st, t);
        }
    }
    return st;
}
 
// Driver code
let s = "xyz";
 
let st = new Set();
st = generate(st, s);
 
let str = '';
console.log(st)
for(item of st.values())
    str += item + '<br> '
     
document.write(str);
 
</script>


Output

x
xy
xyz
xz
y
yz
z

Time Complexity: O(nn)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads