📌  相关文章
📜  拆分字符串以使每个分区以不同字符开头的方法

📅  最后修改于: 2022-05-13 01:57:07.750000             🧑  作者: Mango

拆分字符串以使每个分区以不同字符开头的方法

给定一个字符串s 。令k为给定字符串可能的最大分区数,每个分区以不同的字符开头。任务是找到可以将字符串s 拆分为k个分区(非空)的方式的数量,以便每个分区以不同的字符开始。
例子:

Input : s = "abb"
Output : 2
"abb" can be maximum split into 2 
partitions {a, bb} with distinct 
starting character, so k = 2. And, 
number of ways to split "abb" into 
2 partition with distinct starting 
character is 2 that are {a, bb} and
{ab, b}.

Input : s = "acbbcc"
Output : 6

首先,我们需要找到 k 的值。观察 k 将等于字符串中不同字符的数量,因为只有分区数可以是最大的,这样每个分区都有不同的起始元素。
现在,要找到将字符串拆分为 k 部分的方法数量,每个分区以不同的字符开头。首先观察第一个分区总是固定字符串的第一个字符,不管它有多长。现在,我们需要处理除第一个字符之外的所有其他字符。
让我们举个例子,比如 s = “acbbcc”,我们已经在上面讨论过第一个字符'a'。现在,要处理“b”和“c”,观察“b”出现在字符串中的两个位置,而“c”出现在三个位置。如果我们为“b”选择两个位置中的任何一个,为“c”选择三个位置中的任何一个,那么,我们可以在这些位置对字符串进行分区。请注意,部分的数量将等于 3(等于 s ie k 中不同字符的数量)。
所以概括观察,让计数i是字符i 在 s 中出现的次数。所以我们的答案将是所有 i 的计数i的乘积,使得 i 出现在字符串中并且 i 不等于字符串的第一个字符。
下面是这种方法的实现:

C++
// CPP Program to find number of way
// to split string such that each partition
// starts with distinct character with
// maximum number of partitions.
#include 
 
using namespace std;
 
// Returns the number of we can split
// the string
int countWays(string s)
{
    int count[26] = { 0 };
 
    // Finding the frequency of each
    // character.
    for (char x : s)
        count[x - 'a']++;
 
    // making frequency of first character
    // of string equal to 1.
    count[s[0] - 'a'] = 1;
 
    // Finding the product of frequency
    // of occurrence of each character.
    int ans = 1;
    for (int i = 0; i < 26; ++i)
        if (count[i] != 0)
            ans *= count[i];
 
    return ans;
}
 
// Driven Program
int main()
{
    string s = "acbbcc";
    cout << countWays(s) << endl;
    return 0;
}


Java
// Java Program to find number
// of way to split string such
// that each partition starts
// with distinct character with
// maximum number of partitions.
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG
{
 
// Returns the number of we
// can split the string
static int countWays(String s)
{
    int count[] = new int[26];
 
    // Finding the frequency of
    // each character.
    for (int i = 0; i < s.length(); i++)
        count[s.charAt(i) - 'a']++;
 
    // making frequency of first
    // character of string equal to 1.
    count[s.charAt(0) - 'a'] = 1;
 
    // Finding the product of frequency
    // of occurrence of each character.
    int ans = 1;
    for (int i = 0; i < 26; ++i)
        if (count[i] != 0)
            ans *= count[i];
 
    return ans;
}
 
// Driver Code
public static void main(String ags[])
{
    String s = "acbbcc";
    System.out.println(countWays(s));
}
}
 
// This code is contributed
// by Subhadeep


Python3
# Python3 Program to find number of way
# to split string such that each partition
# starts with distinct character with
# maximum number of partitions.
 
# Returns the number of we can split
# the string
def countWays(s):
    count = [0] * 26;
 
    # Finding the frequency of each
    # character.
    for x in s:
        count[ord(x) -
              ord('a')] = (count[ord(x) -
                                 ord('a')]) + 1;
 
    # making frequency of first character
    # of string equal to 1.
    count[ord(s[0]) - ord('a')] = 1;
 
    # Finding the product of frequency
    # of occurrence of each character.
    ans = 1;
    for i in range(26):
        if (count[i] != 0):
            ans *= count[i];
 
    return ans;
 
# Driver Code
if __name__ == '__main__':
    s = "acbbcc";
    print(countWays(s));
 
# This code is contributed by Rajput-Ji


C#
// C# Program to find number
// of way to split string such
// that each partition starts
// with distinct character with
// maximum number of partitions.
 
using System;
  
class GFG
{
  
// Returns the number of we
// can split the string
static int countWays(string s)
{
    int[] count = new int[26];
  
    // Finding the frequency of
    // each character.
    for (int i = 0; i < s.Length; i++)
        count[s[i] - 'a']++;
  
    // making frequency of first
    // character of string equal to 1.
    count[s[0] - 'a'] = 1;
  
    // Finding the product of frequency
    // of occurrence of each character.
    int ans = 1;
    for (int i = 0; i < 26; ++i)
        if (count[i] != 0)
            ans *= count[i];
  
    return ans;
}
  
// Driver Code
public static void Main()
{
    string s = "acbbcc";
    Console.WriteLine(countWays(s));
}
}


Javascript


输出:
6