Subsequences of given string consisting of non-repeating characters
Last Updated :
29 Jan, 2022
Given a string str of length N, the task is to print all possible distinct subsequences of the string str which consists of non-repeating characters only.
Examples:
Input: str = “abac”
Output: a ab abc ac b ba bac bc c
Explanation:
All possible distinct subsequences of the strings are { a, aa, aac, ab, aba, abac, abc, ac, b, ba, bac, bc, c }
Therefore, the subsequences consisting non-repeating characters only are { a, ab, abc, ac, b, ba, bac, bc, c }
Input: str = “aaa”
Output: a
Approach: The problem can be solved using Backtracking technique using the following recurrence relation:
FindSub(str, res, i) = { FindSub(str, res, i + 1), FindSub(str, res + str[i], i + 1) }
res = subsequence of the string
i = index of a character in str
Follow the steps below to solve the problem:
- Initialize a Set, say sub, to store all possible subsequences consisting of non-repeating characters.
- Initialize another Set, say ch, to check if a character is present in the subsequence or not.
- Traverse the string and print all possible subsequences consisting of non-repeating characters only using the above recurrence relation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void FindSub(set<string>& sub, set< char >& ch, string str,
string res, int i)
{
if (i == str.length()) {
sub.insert(res);
return ;
}
if (!ch.count(str[i])) {
ch.insert(str[i]);
res.push_back(str[i]);
FindSub(sub, ch, str, res, i + 1);
res.pop_back();
ch.erase(str[i]);
}
FindSub(sub, ch, str, res, i + 1);
}
void printSubwithUniqueChar(string str, int N)
{
set<string> sub;
set< char > ch;
FindSub(sub, ch, str, "" , 0);
for ( auto subString : sub) {
cout << subString << " " ;
}
}
int main()
{
string str = "abac" ;
int N = str.length();
printSubwithUniqueChar(str, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void FindSub(HashSet<String> sub,
HashSet<Character> ch,
String str, String res,
int i)
{
if (i == str.length()) {
sub.add(res);
return ;
}
if (!ch.contains(str.charAt(i))) {
ch.add(str.charAt(i));
FindSub(sub, ch, str, res + str.charAt(i),
i + 1 );
ch.remove(str.charAt(i));
}
FindSub(sub, ch, str, res, i + 1 );
}
public static void printSubwithUniqueChar(String str,
int N)
{
HashSet<String> sub = new HashSet<>();
HashSet<Character> ch = new HashSet<>();
FindSub(sub, ch, str, "" , 0 );
for (String subString : sub) {
System.out.print(subString + " " );
}
}
public static void main(String args[])
{
String str = "abac" ;
int N = str.length();
printSubwithUniqueChar(str, N);
}
}
|
Python3
def FindSub(sub, ch1, str1, res, i):
if (i = = len (str1)):
sub.add(res)
return
if (str1[i] not in ch1):
ch1.add(str1[i])
FindSub(sub, ch1, str1, res + str1[i], i + 1 )
res + = str1[i]
res = res[ 0 : len (res) - 1 ]
ch1.remove(str1[i])
FindSub(sub, ch1, str1, res, i + 1 )
def printSubwithUniquech1ar(str1, N):
sub = set ()
ch1 = set ()
FindSub(sub, ch1, str1, "", 0 )
temp = []
for substring in sub:
temp.append(substring)
temp.sort(reverse = False )
for x in temp:
print (x, end = " " )
if __name__ = = '__main__' :
str2 = "abac"
N = len (str2)
printSubwithUniquech1ar(str2, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void FindSub(HashSet<String> sub,
HashSet< char > ch,
String str, String res,
int i)
{
if (i == str.Length)
{
sub.Add(res);
return ;
}
if (!ch.Contains(str[i]))
{
ch.Add(str[i]);
FindSub(sub, ch, str, res + str[i],
i + 1);
ch.Remove(str[i]);
}
FindSub(sub, ch, str, res, i + 1);
}
public static void printSubwithUniqueChar(String str,
int N)
{
HashSet<String> sub = new HashSet<String>();
HashSet< char > ch = new HashSet< char >();
FindSub(sub, ch, str, "" , 0);
foreach (String subString in sub)
{
Console.Write(subString + " " );
}
}
public static void Main(String []args)
{
String str = "abac" ;
int N = str.Length;
printSubwithUniqueChar(str, N);
}
}
|
Javascript
<script>
var sub = new Set();
var ch = new Set();
function FindSub(str, res, i)
{
if (i == str.length) {
sub.add(res.join( "" ));
return ;
}
if (!ch.has(str[i])) {
ch.add(str[i]);
res.push(str[i]);
FindSub(str, res, i + 1);
res.pop()
ch. delete (str[i]);
}
FindSub(str, res, i + 1);
}
function printSubwithUniqueChar(str, N)
{
FindSub(str, [], 0);
var tmp = [...sub].sort();
tmp.forEach(subString => {
document.write( subString + " " );
});
}
var str = "abac" ;
var N = str.length;
printSubwithUniqueChar(str, N);
</script>
|
Output:
a ab abc ac b ba bac bc c
Time complexity: O(N * log(N) * 2N)
Auxiliary Space O(N * 2N)
Share your thoughts in the comments
Please Login to comment...