Skip to content
Related Articles

Related Articles

Improve Article

Count of non-empty sequences of a String

  • Difficulty Level : Medium
  • Last Updated : 07 Jul, 2021

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: 
 

C++




// 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';
 
    // uncomment following to print all generated strings
    /*
    for(auto i: se)
            cout<<i<<endl;
    */
 
    return 0;
}

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

Javascript




<script>
// Javascript program for
// the above approach
  
// Recursive function which will
// calculate all the possibilities
// recursively
 
function recurr(vis, ans)
{
 
    // Check that the string
    // is already there or not
    if(ans.length>0)
    {
        if(se.has(ans))
        {
            return;
        }
         
        // Else put in set
        se.add(ans);
    }
    // Run for all the
    // possibilities
    for(let i=0;i<tiles.length;i++)
    {
        // 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
 
let tiles = "AAABBC";
let curr = "";
 
let se = new Set();
let vis = new Array(tiles.length);
for(let i = 0; i < tiles.length; i++)
{
    vis[i] = false;
}
 
recurr(vis, curr);
document.write(se.size);
 
// This code is contributed by ab2127
</script>
Output
188

Another Approach: The idea is to maintain the frequency of the characters of the string and then by choosing each character of the string increment the count by one and decrement the frequency and Recursively call for the rest of the frequencies of the characters.

Below is the implementation of the above approach:



C++




// C++ implementation of the
// above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the count
// of the number of strings
void countNumberOfStringsUtil(
    vector<int>& freq, int& count)
{
    // Loop to iterate over the
    // frequency of character of string
    for (int i = 0; i < 26; i++) {
        if (freq[i] > 0) {
            // reduce the frequency of
            // current element
            freq[i]--;
            count++;
           
            // recursive call
            countNumberOfStringsUtil(freq,count);
            freq[i]++; // backtrack
        }
    }
}
 
// Function to count the number of
// non-empty sequences
int countNumberOfStrings(string s) {
    // store the frequency of each character
    vector<int> freq(26, 0);
   
    // Maintain the frequency
    for (int i = 0; i < s.size(); i++) {
        freq[s[i] - 'A']++;
    }
   
    int count = 0;
    countNumberOfStringsUtil(freq, count);
   
    return count;
}
 
// Driver Code
int main()
{
 
    string s = "AAABBC";
     
    // Function Call
    cout << countNumberOfStrings(s);
    return 0;
}

Java




// Java implementation of the
// above approach
import java.io.*;
import java.util.*;
 
class GFG
{
  public static int count = 0;
 
  // Function to find the count
  // of the number of strings
  public static void countNumberOfStringsUtil(int[] freq)
  {
 
    // Loop to iterate over the
    // frequency of character of string
    for(int i = 0; i < 26; i++)
    {
      if(freq[i] > 0)
      {
 
        // reduce the frequency of
        // current element
        freq[i]--;
        count++;
 
        // recursive call
        countNumberOfStringsUtil(freq);
        freq[i]++;// backtrack
      }
    }       
  }
 
  // Function to count the number of
  // non-empty sequences
  public static int countNumberOfStrings(String s)
  {
 
    // store the frequency of each character
    int[] freq = new int[26];
    Arrays.fill(freq, 0);
 
    // Maintain the frequency
    for(int i = 0; i < s.length(); i++)
    {
      freq[s.charAt(i) - 'A']++;
    }
 
    countNumberOfStringsUtil(freq);
    return count;
  }
   
  // Driver Code
  public static void main (String[] args)
  {
    String s = "AAABBC";
 
    // Function Call
    System.out.println(countNumberOfStrings(s));
  }
}
 
// This code is contributed by rag2127

Python3




# Python3 implementation of the
# above approach
count = 0
 
# Function to find the count
# of the number of strings
def countNumberOfStringsUtil(freq,
                             Count):
    global count
    count = Count
 
    # Loop to iterate over the
    # frequency of character of string
    for i in range(26):
        if(freq[i] > 0):
 
            # Reduce the frequency of
            # current element
            freq[i] -= 1
            count+=1
 
            # Recursive call
            countNumberOfStringsUtil(freq,
                                     count);
            # Backtrack
            freq[i] += 1
 
# Function to count the number of
# non-empty sequences
def countNumberOfStrings(s):
   
    global count
    global freq
 
    # store the frequency
    # of each character
    freq = [0 for i in range(26)]
 
    # Maintain the frequency
    for i in range(len(s)):
        freq[ord(s[i]) -
             ord('A')] += 1
 
    countNumberOfStringsUtil(freq,
                             count);
    return count
 
# Driver Code
s = "AAABBC"
 
# Function Call
print(countNumberOfStrings(s))
 
# This code is contributed by avanitrachhadiya2155

Javascript




<script>
// Javascript implementation of the
// above approach
let count = 0;
 
// Function to find the count
  // of the number of strings
function countNumberOfStringsUtil(freq)
{
    // Loop to iterate over the
    // frequency of character of string
    for(let i = 0; i < 26; i++)
    {
      if(freq[i] > 0)
      {
  
        // reduce the frequency of
        // current element
        freq[i]--;
        count++;
  
        // recursive call
        countNumberOfStringsUtil(freq);
        freq[i]++;// backtrack
      }
    }      
}
 
// Function to count the number of
  // non-empty sequences
function countNumberOfStrings(s)
{
 
    // store the frequency of each character
    let freq = new Array(26);
    for(let i = 0; i < freq.length; i++)
    {   
        freq[i] = 0;
    }
     
  
    // Maintain the frequency
    for(let i = 0; i < s.length; i++)
    {
      freq[s[i].charCodeAt(0) - 'A'.charCodeAt(0)]++;
    }
  
    countNumberOfStringsUtil(freq);
    return count;
}
 
// Driver Code
let s = "AAABBC";
  
// Function Call
document.write(countNumberOfStrings(s));
 
// This code is contributed by unknown2108
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :