Count of non-empty sequences of a String - GeeksforGeeks

20 Dec, 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++
Java
Python
C#
Javascript
// C++ program for// the above approach#include <bits/stdc++.h>using namespace std; // Recursive function which will// calculate all the possibilities// recursivelyvoid 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 codeint 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;}
// Java program for the above approachimport java.util.*;public class Main{    static HashSet<String> se = new HashSet<String>();    static String tiles;         // Recursive function which will    // calculate all the possibilities    // recursively    static void recurr(Vector<Boolean> vis, String ans)    {               // Check that the string        // is already there or not        if(ans.length()>0)        {            if(se.contains(ans))            {                return;            }                           // Else put in set            se.add(ans);        }                // Run for all the        // possibilities        for(int i = 0; i < tiles.length(); i++)        {            // If already taken            // then don't do anything            if (vis.get(i))                continue;            vis.set(i, true);                           // Else take it and            //call recurr function            recurr(vis, ans + tiles.charAt(i));            vis.set(i, false);        }    }         public static void main(String[] args)    {        tiles = "AAABBC";        String curr = "";                   Vector<Boolean> vis = new Vector<Boolean>();        for(int i = 0; i < tiles.length(); i++)        {            vis.add(false);        }                   recurr(vis, curr);        System.out.print(se.size());    }} // This code is contributed by rameshtravel07.
# 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 codetiles = "AAABBC"curr = "" se = dict()vis = [False] * (len(tiles))recurr(vis, curr)print(len(se)) # This code is contributed by mohit kumar 29
// C# program for the above approachusing System;using System.Collections.Generic;class GFG {         static HashSet<string> se = new HashSet<string>();    static string tiles;         // Recursive function which will    // calculate all the possibilities    // recursively    static void recurr(List<bool> vis, string ans)    {              // Check that the string        // is already there or not        if(ans.Length>0)        {            if(se.Contains(ans))            {                return;            }                          // Else put in set            se.Add(ans);        }               // Run for all the        // possibilities        for(int 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;        }    }   static void Main() {    tiles = "AAABBC";    string curr = "";          List<bool> vis = new List<bool>();    for(int i = 0; i < tiles.Length; i++)    {        vis.Add(false);    }          recurr(vis, curr);    Console.WriteLine(se.Count);  }} // This code is contributed by divyesh072019.
<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>
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++
Java
Python3
C#
Javascript
// C++ implementation of the// above approach #include <bits/stdc++.h>using namespace std; // Function to find the count// of the number of stringsvoid 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 sequencesint 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 Codeint main(){     string s = "AAABBC";         // Function Call    cout << countNumberOfStrings(s);    return 0;}
// Java implementation of the// above approachimport 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 implementation of the# above approachcount = 0 # Function to find the count# of the number of stringsdef 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 sequencesdef 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 Codes = "AAABBC" # Function Callprint(countNumberOfStrings(s)) # This code is contributed by avanitrachhadiya2155
// C# implementation of the// above approachusing System;using System.Collections.Generic;class GFG {        static int count = 0;    // Function to find the count  // of the number of strings  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];    Array.Fill(freq, 0);      // Maintain the frequency    for(int i = 0; i < s.Length; i++)    {      freq[s[i] - 'A']++;    }      countNumberOfStringsUtil(freq);    return count;  }     static void Main() {    string s = "AAABBC";      // Function Call    Console.Write(countNumberOfStrings(s));  }} // This code is contributed by suresh07.
<script>// Javascript implementation of the// above approachlet count = 0; // Function to find the count  // of the number of stringsfunction 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 sequencesfunction 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 Codelet s = "AAABBC";  // Function Calldocument.write(countNumberOfStrings(s)); // This code is contributed by unknown2108</script>
188
mohit kumar 29
mazeoz
avanitrachhadiya2155
rag2127
unknown2108
ab2127
suresh07
divyesh072019
rameshtravel07
Recursion
Strings
Technical Scripter
Strings
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Practice Questions for Recursion | Set 1
Sum of natural numbers using recursion
Recursively Reversing a linked list (A simple implementation)
Generating subarrays using recursion
Recursive Insertion Sort
Write a program to reverse an array or string
Reverse a string in Java
C++ Data Types
Longest Common Subsequence | DP-4
Check for Balanced Brackets in an expression (well-formedness) using Stack