字符频率大于其他字符频率之和的字符串连接计数
我们这里的主要目标是确定可以连接的最大字符串数量,以确保在名为 arr[] 的包含 M 个字符串的数组中,只有一个字母的频率超过所有其他字符的总和。
在继续之前,让我们了解一些数组和字符串的基本概念。
数组不过是一组数据类型相同的元素,存储在连续的内存段中。
C 编程语言中的数组具有固定大小,这意味着一旦指定了大小,就无法更改;您无法缩小或扩展它。
现在让我们检查一下什么是字符串。字符串是一组字符,在 C 编程语言中以空字符“\0”结尾。来自 C 字符串的字符存储在字符数组中。C 字符串与字符数组的区别在于,它以独特的空字符结尾,而字符数组则没有。
问题陈述
实现一个程序来确定需要连接的字符串数量,以使一个字符的频率大于其他字符的频率之和。
示例 1
Let us take the input array
arr[]: {“xyz", “yyyyx", “q”}
Output obtained is: 3
解释
这里元素“x”的频率为 2。
元素“y”的频率为 5,元素“z”的频率为 1。最后,
字符“q”的频率为 1。
通过连接数组中的所有三个字符串,我们得到“xyzyyyyxq”。
这里字符‘y’的频率为 5,其余字符频率之和为 4。
示例 2
Let us take the input array
arr[]: {“mnoml", “lmll", “nln”, "mnlmn"}
Output obtained is : 2
解释
这里元素或字符“m”的频率为 5。
元素“n”的频率为 5,元素“l”的频率为 6,最后字符“o”的频率为 1。
这里我们只能连接 2 个字符串“lmllnl”。
这里字符 l 的频率为 4。其他字符 m 和 n 的频率之和为 2。为了依赖于具有频率大于其他字符频率之和的字符的连接字符串,这是唯一可能的连接。
方法
为了确定需要连接的字符串数量,以使一个字符的频率大于其他字符的频率之和,我们采用以下方法。
解决此问题并获得需要连接的字符串数量的方法,以使一个字符的频率大于其他字符的频率之和,我们进行迭代。
也就是说,我们通过迭代所有字符(即从“a”到“z”)来确定所有字符串中每个字符的净频率。在这种情况下,净频率可以通过从中减去所有其他频率来计算,因此,如果总净频率大于 0,则表示该元素的频率超过所有其他频率的总和。
算法
确定需要连接的字符串数量的算法,以使一个字符的频率大于其他字符的频率之和,如下所示。
步骤 1 - 开始
步骤 2 - 定义一个函数来确定所有字符的频率并减少字符串中其他频率的总和。
步骤 3 - 迭代字符串数组 a
步骤 4 - 定义一个整数变量来存储频率
步骤 5 - 将频率存储在数组 v 中
步骤 6 - 定义一个变量来存储最大计数
步骤 7 - 遍历所有字母或元素
步骤 8 - 返回最大值
步骤 9 - 停止
示例:C 程序
以下是上述方法的 C 程序实现,以获得需要连接的字符串数量,以使一个字符的频率大于其他字符的频率之和。
#include <stdio.h>
#include <stdlib.h>
//input strings to be non-empty and not more //than 100 characters
#define MAX_STR_LEN 100
// Function to determine the frequencies of all the characters and reducing the sum of other frequencies in the strings
int* frequency(char** a, int len, char c){
// We use array to store the frequency
int* v = (int*)calloc(len, sizeof(int));
if(v == NULL) {
printf("Error: Memory allocation failed");
exit(1);
}
// Iterating the array a of strings
for (int i = 0; i < len; i++) {
char* str = a[i];
// defining an integer variable for storing //the frequencies
int net_fre = 0;
// Iterating through the string str
for (int j = 0; str[j] != '\0'; j++) {
// If str[j] is equal to the current character increment the net_fre by 1
if (str[j] == c)
net_fre++;
// otherwise decrement net_fre by 1
else
net_fre--;
}
// After iterating the string store this frequency in the array v
v[i] = net_fre;
}
//return the array v
return v;
}
// Function to determine the count of the longest or the lengthiest string that could be obtained from the given array of strings
int longestConcatenatedString(char** a, int len){
// An integer variable to store the maximum count Also it is set to zero
int mxm = 0;
// Iterating through all of the alphabets
for (char c = 'a'; c <= 'z'; c++) {
// Array to store the net_frequency of the character c after reducing the sum of every other frequencies in all of the strings
int* v = frequency(a, len, c);
// Array is stored in the order of descendants
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (v[i] < v[j]) {
int temp = v[i];
v[i] = v[j];
v[j] = temp;
char* temp_str = a[i];
a[i] = a[j];
a[j] = temp_str;
}
}
}
// Variable res is defined to store the //result
int res = 0;
int sum = 0;
for (int i = 0; i < len; i++) {
sum += v[i];
// If sum is greater than 0 then increment res by 1
if (sum > 0) {
res++;
}
}
// Keeping the track of the maximum one
mxm = mxm > res ? mxm : res;
free(v);
}
// Returning the maximum value obtained
return mxm;
}
int main(){
char* a[] = { "mnoml", "lmll", "nln", "mnlmn" };
printf("Count of strings to be concatenated with a character having frequency greater than sum of others: ");
int len = sizeof(a) / sizeof(a[0]);
printf("%d", longestConcatenatedString(a, len));
return 0;
}
输出
Count of strings to be concatenated with a character having frequency greater than sum of others: 2
结论
同样,我们可以计算需要连接的字符串数量,以使一个字符的频率大于其他字符的频率之和。
本文解决了获取程序以计算需要连接的字符串数量的问题,以使一个字符的频率大于其他字符的频率之和。
这里提供了 C++ 编程代码以及确定需要连接的字符串数量的算法,以使一个字符的频率大于其他字符的频率之和。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP