给定长度为N的字符串str ,任务是打印字符串str 的所有可能的不同子序列,这些子序列仅由非重复字符组成。
例子:
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
方法:该问题可以使用回溯技术使用以下递推关系解决:
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
请按照以下步骤解决问题:
- 初始化一个 Set,比如说sub ,以存储由非重复字符组成的所有可能的子序列。
- 初始化另一个 Set,例如ch ,以检查子序列中是否存在字符。
- 仅使用上述递归关系遍历字符串并打印所有可能的由非重复字符组成的子序列。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find all the subsequences of
// the string with non-repeating characters
void FindSub(set& sub, set& 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 sub;
// Stores subsequence with
// non-repeating characters
set 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 sub,
HashSet 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 sub = new HashSet<>();
// Stores subsequence with
// non-repeating characters
HashSet 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 str1ing with non-repeating ch1aracters
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 str1ing with non-repeating ch1aracters
def printSubwithUniquech1ar(str1, N):
# Stores all possible subsequences
# with non-repeating ch1aracters
sub = set()
# Stores subsequence with
# non-repeating ch1aracters
ch1 = set()
FindSub(sub, ch1, str1, "", 0)
# Traverse all possible subsequences
# containing non-repeating ch1aracters
temp = []
for substr1ing in sub:
temp.append(substr1ing)
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 sub,
HashSet 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 sub = new HashSet();
// Stores subsequence with
// non-repeating characters
HashSet ch = new HashSet();
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
a ab abc ac b ba bac bc c
时间复杂度:O(N * log(N) * 2 N )
辅助空间O(N * 2 N )
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。