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++
#include <bits/stdc++.h>
using namespace std;
void generate(set<string>& st, string s)
{
if (s.size() == 0)
return ;
if (st.find(s) == st.end()) {
st.insert(s);
for ( int i = 0; i < s.size(); i++) {
string t = s;
t.erase(i, 1);
generate(st, t);
}
}
return ;
}
int main()
{
string s = "xyz" ;
set<string> st;
set<string>::iterator it;
generate(st, s);
for ( auto it = st.begin(); it != st.end(); it++)
cout << *it << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static void generate(Set<String> st, String s)
{
if (s.length() == 0 ) {
return ;
}
if (!st.contains(s)) {
st.add(s);
for ( int i = 0 ; i < s.length(); i++) {
String t = s;
t = t.substring( 0 , i) + t.substring(i + 1 );
generate(st, t);
}
}
return ;
}
public static void main(String args[])
{
String s = "xyz" ;
TreeSet<String> st = new TreeSet<>();
generate(st, s);
for (String str : st) {
System.out.println(str);
}
}
}
|
Python 3
def generate(st, s):
if len (s) = = 0 :
return
if s not in st:
st.add(s)
for i in range ( len (s)):
t = list (s).copy()
t.remove(s[i])
t = ''.join(t)
generate(st, t)
return
if __name__ = = "__main__" :
s = "xyz"
st = set ()
generate(st, s)
for i in st:
print (i)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void generate(HashSet<String> st, String s)
{
if (s.Length == 0) {
return ;
}
if (!st.Contains(s)) {
st.Add(s);
for ( int i = 0; i < s.Length; i++) {
String t = s;
t = t.Substring(0, i) + t.Substring(i + 1);
generate(st, t);
}
}
return ;
}
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);
}
}
}
|
Javascript
<script>
function generate(st,s){
if (s.length == 0)
return st;
if (!st.has(s)) {
st.add(s);
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;
}
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)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Mar, 2023
Like Article
Save Article