Count of non-empty sequences of a String

Given a string s, the task is to find the number of possible non-empty sequences of letters that can be made.

Examples:

Input: "AAB"
Output: 8 
Explanation:
1) A
2) AA
3) AAB
4) AB
5) ABA
6) B
7) BA
8) BAA
Total 8 possibilities

Input: "AAABBC"
Output: 188

Approach:
There are two possibilities either take the current character to our answer or leave it. We can solve this problem
To check for duplicates we can take set as a data Structure and will put our answers there and our count will be the size of our set.



Below is the implementation of the above approach:

CPP

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for
// the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Recursive function which will
// calculate all the possibilities
// recursively
void recurr(string& tiles, vector<bool> vis, string ans,
            set<string>& se)
{
    if (ans.size() > 0) {
        // Check that the string
        // is already there or not
        if (se.count(ans))
            return;
        // Else put in set
        se.insert(ans);
    }
    // Run for all the
    // possibilities
    for (int i = 0; i < tiles.size(); i++) {
        // If already taken
        // then don't do anything
        if (vis[i])
            continue;
        vis[i] = true;
        // Else take it and
        // call recurr function
        recurr(tiles, vis, ans + tiles[i], se);
        vis[i] = false;
    }
}
  
// Driver code
int main()
{
  
    string s = "AAABBC";
    string curr = "";
  
    set<string> se;
    vector<bool> vis(s.size(), false);
    recurr(s, vis, curr, se);
  
    int ans = se.size();
    cout << ans << '\n';
  
    return 0;
}

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for
# the above approach
  
# Recursive function which will
# calculate all the possibilities
# recursively
def recurr(vis, ans):
    global tiles, se
    if (len(ans) > 0):
          
        # Check that the string
        # is already there or not
        if (ans in se):
            return
              
        # Else put in set
        se[ans] = 1
          
    # Run for all the
    # possibilities
    for i in range(len(tiles)):
          
        # If already taken
        # then don't do anything
        if (vis[i]):
            continue
        vis[i] = True
          
        # Else take it and
        # call recurr function
        recurr(vis, ans + tiles[i])
        vis[i] = False
  
# Driver code
tiles = "AAABBC"
curr = ""
  
se = dict()
vis = [False] * (len(tiles))
recurr(vis, curr)
print(len(se))
  
# This code is contributed by mohit kumar 29

chevron_right


Output:

188


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : mohit kumar 29