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++
// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find all the subsequences of
// the string with non-repeating characters
void FindSub(set<string>& sub, set<char>& ch, string str,
string res, int i)
{
// Base case
if (i == str.length()) {
// Insert current subsequence
sub.insert(res);
return;
}
// If str[i] is not present
// in the current subsequence
if (!ch.count(str[i])) {
// Insert str[i] into the set
ch.insert(str[i]);
// Insert str[i] into the
// current subsequence
res.push_back(str[i]);
FindSub(sub, ch, str, res, i + 1);
// Remove str[i] from
// current subsequence
res.pop_back();
// Remove str[i] from the set
ch.erase(str[i]);
}
// Not including str[i] from
// the current subsequence
FindSub(sub, ch, str, res, i + 1);
}
// Utility function to print all subsequences
// of string with non-repeating characters
void printSubwithUniqueChar(string str, int N)
{
// Stores all possible subsequences
// with non-repeating characters
set<string> sub;
// Stores subsequence with
// non-repeating characters
set<char> ch;
FindSub(sub, ch, str, "", 0);
// Traverse all possible subsequences
// containing non-repeating characters
for (auto subString : sub) {
// Print subsequence
cout << subString << " ";
}
}
// Driver Code
int main()
{
string str = "abac";
int N = str.length();
printSubwithUniqueChar(str, N);
return 0;
}
Java
// Javs program to implement
// the above approach
import java.util.*;
class GFG {
// Function to find all the subsequences of
// the string with non-repeating characters
public static void FindSub(HashSet<String> sub,
HashSet<Character> ch,
String str, String res,
int i)
{
// Base case
if (i == str.length()) {
// Insert current subsequence
sub.add(res);
return;
}
// If str[i] is not present
// in the current subsequence
if (!ch.contains(str.charAt(i))) {
// Insert str[i] into the set
ch.add(str.charAt(i));
// Insert str[i] into the
// current subsequence
FindSub(sub, ch, str, res + str.charAt(i),
i + 1);
// Remove str[i] from the set
ch.remove(str.charAt(i));
}
// Not including str[i] from
// the current subsequence
FindSub(sub, ch, str, res, i + 1);
}
// Utility function to print all subsequences
// of string with non-repeating characters
public static void printSubwithUniqueChar(String str,
int N)
{
// Stores all possible subsequences
// with non-repeating characters
HashSet<String> sub = new HashSet<>();
// Stores subsequence with
// non-repeating characters
HashSet<Character> ch = new HashSet<>();
FindSub(sub, ch, str, "", 0);
// Traverse all possible subsequences
// containing non-repeating characters
for (String subString : sub) {
// Print subsequence
System.out.print(subString + " ");
}
}
// Driver Code
public static void main(String args[])
{
String str = "abac";
int N = str.length();
printSubwithUniqueChar(str, N);
}
}
Python3
# Python 3 program to implement
# the above approach1
# Function to find all the subsequences of
# the string with non-repeating characters
def FindSub(sub, ch1, str1, res, i):
# Base case
if (i == len(str1)):
# Insert current subsequence
sub.add(res)
return
# If str1[i] is not present
# in the current subsequence
if (str1[i] not in ch1):
# Insert str1[i] into the set
ch1.add(str1[i])
# Insert str1[i] into the
# current subsequence
FindSub(sub, ch1, str1, res+str1[i], i + 1)
res += str1[i]
# Remove str1[i] from
# current subsequence
res = res[0:len(res) - 1]
# Remove str1[i] from the set
ch1.remove(str1[i])
# Not including str1[i] from
# the current subsequence
FindSub(sub, ch1, str1, res, i + 1)
# Utility function to print all subsequences
# of string with non-repeating characters
def printSubwithUniquech1ar(str1, N):
# Stores all possible subsequences
# with non-repeating characters
sub = set()
# Stores subsequence with
# non-repeating characters
ch1 = set()
FindSub(sub, ch1, str1, "", 0)
# Traverse all possible subsequences
# containing non-repeating characters
temp = []
for substring in sub:
temp.append(substring)
temp.sort(reverse = False)
for x in temp:
# Print subsequence
print(x, end = " ")
# Driver Code
if __name__ == '__main__':
str2 = "abac"
N = len(str2)
printSubwithUniquech1ar(str2, N)
# This code is contributed by bgangwar59.
C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to find all the subsequences of
// the string with non-repeating characters
public static void FindSub(HashSet<String> sub,
HashSet<char> ch,
String str, String res,
int i)
{
// Base case
if (i == str.Length)
{
// Insert current subsequence
sub.Add(res);
return;
}
// If str[i] is not present
// in the current subsequence
if (!ch.Contains(str[i]))
{
// Insert str[i] into the set
ch.Add(str[i]);
// Insert str[i] into the
// current subsequence
FindSub(sub, ch, str, res + str[i],
i + 1);
// Remove str[i] from the set
ch.Remove(str[i]);
}
// Not including str[i] from
// the current subsequence
FindSub(sub, ch, str, res, i + 1);
}
// Utility function to print all subsequences
// of string with non-repeating characters
public static void printSubwithUniqueChar(String str,
int N)
{
// Stores all possible subsequences
// with non-repeating characters
HashSet<String> sub = new HashSet<String>();
// Stores subsequence with
// non-repeating characters
HashSet<char> ch = new HashSet<char>();
FindSub(sub, ch, str, "", 0);
// Traverse all possible subsequences
// containing non-repeating characters
foreach (String subString in sub)
{
// Print subsequence
Console.Write(subString + " ");
}
}
// Driver Code
public static void Main(String []args)
{
String str = "abac";
int N = str.Length;
printSubwithUniqueChar(str, N);
}
}
// This code contributed by shikhasingrajput
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Stores all possible subsequences
// with non-repeating characters
var sub = new Set();
// Stores subsequence with
// non-repeating characters
var ch = new Set();
// Function to find all the subsequences of
// the string with non-repeating characters
function FindSub(str, res, i)
{
// Base case
if (i == str.length) {
// Insert current subsequence
sub.add(res.join(""));
return;
}
// If str[i] is not present
// in the current subsequence
if (!ch.has(str[i])) {
// Insert str[i] into the set
ch.add(str[i]);
// Insert str[i] into the
// current subsequence
res.push(str[i]);
FindSub(str, res, i + 1);
res.pop()
// Remove str[i] from the set
ch.delete(str[i]);
}
// Not including str[i] from
// the current subsequence
FindSub(str, res, i + 1);
}
// Utility function to print all subsequences
// of string with non-repeating characters
function printSubwithUniqueChar(str, N)
{
FindSub(str, [], 0);
var tmp = [...sub].sort();
// Traverse all possible subsequences
// containing non-repeating characters
tmp.forEach(subString => {
// Print subsequence
document.write( subString + " ");
});
}
// Driver Code
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)
Similar Reads
First non-repeating character of given string
Given a string s of lowercase English letters, the task is to find the first non-repeating character. If there is no such character, return '$'.Examples: Input: s = "geeksforgeeks"Output: 'f'Explanation: 'f' is the first character in the string which does not repeat.Input: s = "racecar"Output: 'e'Ex
9 min read
Count number of substrings of a string consisting of same characters
Given a string. The task is to find out the number of substrings consisting of the same characters. Examples: Input: abba Output: 5 The desired substrings are {a}, {b}, {b}, {a}, {bb} Input: bbbcbb Output: 10 Approach: It is known for a string of length n, there are a total of n*(n+1)/2 number of su
6 min read
Minimum removal of subsequences of distinct consecutive characters required to empty a given string
Given a binary string, str, the task is to empty the given string by minimum number of removals of a single character or a subsequence containing distinct consecutive characters from str. Examples: Input: str = "0100100111" Output: 3 Explanation: Removing the subsequence "010101" from the string mod
6 min read
Count of all unique substrings with non-repeating characters
Given a string str consisting of lowercase characters, the task is to find the total number of unique substrings with non-repeating characters. Examples: Input: str = "abba" Output: 4 Explanation: There are 4 unique substrings. They are: "a", "ab", "b", "ba". Input: str = "acbacbacaa" Output: 10 App
6 min read
Count of sub-strings that do not consist of the given character
Given a string str and a character c. The task is to find the number of sub-strings that do not consist of the character c. Examples: Input: str = "baa", c = 'b' Output: 3 The sub-strings are "a", "a" and "aa" Input: str = "ababaa", C = 'b' Output: 5 Approach: Initially take a counter that counts th
12 min read
Longest Common Subsequence with no repeating character
Given two strings s1 and s2, the task is to find the length of the longest common subsequence with no repeating character. Examples: Input: s1= "aabbcc", s2= "aabc"Output: 3Explanation: "aabc" is longest common subsequence but it has two repeating character 'a'.So the required longest common subsequ
10 min read
First non-repeating character using one traversal of string | Set 2
Given a string, find the first non-repeating character in it. For example, if the input string is "GeeksforGeeks", then output should be 'f' and if input string is "GeeksQuiz", then output should be 'G'. Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. We have dis
15+ min read
Count of unique Subsequences of given String with lengths in range [0, N]
Given a string S of length N, the task is to find the number of unique subsequences of the string for each length from 0 to N. Note: The uppercase letters and lowercase letters are considered different and the result may be large so print it modulo 1000000007. Examples: Input: S = "ababd"Output: Num
14 min read
Find number of times a string occurs as a subsequence in given string
Given two strings, find the number of times the second string occurs in the first string, whether continuous or discontinuous.Examples: Input: string a = "GeeksforGeeks"string b = "Gks"Output: 4Explanation: The four strings are - (Check characters marked in bold)GeeksforGeeksGeeksforGeeksGeeksforGee
15+ min read
Count All Palindromic Subsequence in a given String
Given a string s of length n, the task is to count number of palindromic subsequence (need not necessarily be distinct) present in the string s.Example: Input: s = "abcd"Output: 4Explanation: Palindromic subsequence are : "a" ,"b", "c" ,"d"Input: s = "aab"Output: 4Explanation: palindromic subsequenc
15+ min read