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:8Explanation:1) A 2) AA 3) AAB 4) AB 5) ABA 6) B 7) BA 8) BAA Total 8 possibilitiesInput:"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

`// 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*

*filter_none*

## Python

`# 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*

*filter_none*

**Output:**

188

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of sub-sequences which satisfy the given condition
- Count all sub-sequences having product <= K - Recursive approach
- Count pairs of parentheses sequences such that parentheses are balanced
- Count sequences of length K having each term divisible by its preceding term
- Count distinct regular bracket sequences which are not N periodic
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Check if two same sub-sequences exist in a string or not
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Print all increasing sequences of length k from first n natural numbers
- Print all non-increasing sequences of sum equal to a given number x
- Find all even length binary sequences with same sum of first and second half bits
- Print all longest common sub-sequences in lexicographical order
- Mutual Recursion with example of Hofstadter Female and Male sequences
- Print all sequences starting with n and consecutive difference limited to k
- Distinct strings such that they contains given strings as sub-sequences
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Check if count of Alphabets and count of Numbers are equal in the given String
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count subsequences in first string which are anagrams of the second string
- Find the count of palindromic sub-string of a string in its sorted form

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.