Sudo Placement | Special Subsequences

Given a non-empty string S containing only lowercase letters, print all ‘Special Subsequences’ of S. For Instance, “ab” has the following Special Subsequences-: { “A”, “AB”, “Ab”, “B”, “a”, “aB”, “ab”, “b” }.
Note : Consider only the non-empty special Subsequences of S.

Examples:

Input : S = "a"
Output : { "A", "a" }

Input : S = "ab"
Output : { "A", "AB", "Ab", "B", "a", "aB", "ab", "b" }

Prerequisite : Subsequences of String



Approach : This is a slight variation, to the classic problem, printing the subsequences of a string. Let’s say we have a recursive function generateSubsequences(input, i) that prints all the special subsequences of input string upto ith position. Let the current position be i in the input string, then there are three possibilities:

  1. Include input[i] in the output string as it is and call generateSubsequences(input, i + 1)
  2. Exclude input[i] in the output string and call generateSubsequence(str, i + 1), or
  3. Include the upper case form of input[i] and call generateSubsequences(str, i + 1). Do remember to first remove the currently added character from the output string.

Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find all special subsequences
// of the given type
#include <bits/stdc++.h>
using namespace std;
  
// Function to generate the required subsequences
void generateSubsequences(string input, string output,
                          int idx, vector<string>& ans)
{
    // If the index pointer has reached the
    // end of input string
    if (input[idx] == '\0') {
        // Skip empty (" ") subsequence
        if (output.size())
            ans.push_back(output);
        return;
    }
  
    // Exclude current character in output string
    generateSubsequences(input, output, idx + 1, ans);
  
    // Include current character in output string
    output += input[idx];
    generateSubsequences(input, output, idx + 1, ans);
  
    // Remove the current included character and
    // and include it in its uppercase form
    output.pop_back();
    char upper = input[idx];
    upper = toupper(upper);
  
    output += upper;
    generateSubsequences(input, output, idx + 1, ans);
}
  
// Function to print the required subsequences
void printSubsequences(string S)
{
    // Output String to store every subsequence
    string output;
  
    // Set of strings that will store all special
    // subsequences in lexicographical sorted order
    vector<string> ans;
    generateSubsequences(S, output, 0, ans);
  
    // Sort the strings to print in sorted order
    sort(ans.begin(), ans.end());
  
    for (auto str : ans)
        cout << str << " ";
}
  
// Driver Code
int main()
{
    string S = "ab";
    printSubsequences(S);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find all special subsequences
// of the given type
import java.util.*;
import java.io.*;
  
public class SpecialSubsequences {
  
    // Function to generate the required subsequences
    public static void generateSubsequences(String input, String output,
                                        int index, ArrayList<String> ans)
    {
  
        // If the index pointer has reached the
        // end of input string
        if(index == input.length()){
  
            // Skip empty (" ") subsequence
            if(output.length() != 0){
                ans.add(output);
            }
            return;
        }
  
        //Exclude the character in the output string
        generateSubsequences(input, output, index+1, ans);
  
        //Include the current character as it is
        output += String.valueOf(input.charAt(index));
        generateSubsequences(input, output, index+1, ans);
  
        //Include the current character in its upper case form
        //To remove the last character, we generate a substring till the
        //second last character
        output = output.substring(0, output.length()-1);
        output += String.valueOf(
                    Character.toUpperCase(input.charAt(index)));
        generateSubsequences(input, output, index+1, ans);
    }
  
  
    // Function to print the required subsequences
    public static void printSubsequences(String s){
  
        // Output String to store every subsequence
        String output = "";
  
        // Set of strings that will store all special
        // subsequences in lexicographical sorted order
        ArrayList<String> ans = new ArrayList<>();
        generateSubsequences(s, output, 0, ans);
  
        //Sort the strings to print in the sorted order
        Collections.sort(ans);
  
        for(String str: ans){
            System.out.print(str+" ");
        }
        System.out.println();
    }
  
    //Driver code
    public static void main(String[] args){
            String s = "ab";
            printSubsequences(s);
    }
  
}

chevron_right



Output :

A AB Ab B a aB ab b 

Time Complexity : O(3N), where N is the size of string.



My Personal Notes arrow_drop_up

This article is contributed by Team GeeksforGeeks. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.