📌  相关文章
📜  字符串的非空序列的计数

📅  最后修改于: 2021-05-04 18:16:49             🧑  作者: Mango

给定字符串s,任务是查找可以进行的字母的可能非空序列的数量。
例子:

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

方法:有两种可能性要么将当前字符用作我们的答案,要么将其保留。我们可以解决这个问题
为了检查重复项,我们可以将集合作为数据结构,并将答案放在那里,而我们的数量就是集合的大小。
下面是上述方法的实现:

C++
// C++ program for
// the above approach
#include 
using namespace std;
 
// Recursive function which will
// calculate all the possibilities
// recursively
void recurr(string& tiles, vector vis, string ans,
            set& 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 se;
    vector 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<


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


C++
// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
// Function to find the count
// of the number of strings
void countNumberOfStringsUtil(
    vector& 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 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


输出
188

另一种方法:这样做是为了保持字符串中的字符的频率,然后通过选择字符串的每个字符由一个增加计数和减少的频率和递归调用的字符的频率的其余部分。

下面是上述方法的实现:

C++

// C++ implementation of the
// above approach
 
#include 
using namespace std;
 
// Function to find the count
// of the number of strings
void countNumberOfStringsUtil(
    vector& 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 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
输出
188