字符串中存在的另一个字符串字符频率之和
在本文中,我们将探讨一个与使用各种编程语言进行字符串操作相关的有趣问题。问题陈述是“字符串中存在的另一个字符串字符频率之和”。此问题提供了一个极好的机会来增强您对字符串操作、字符频率计算以及 C、C++、Java 和 Python 中映射概念的理解。
问题陈述
给定两个字符串,任务是找到第一个字符串中存在于第二个字符串中的字符的频率之和。
解决方案方法
为了解决这个问题,我们将首先使用哈希映射为两个字符串创建频率映射。频率映射是一个映射,其中字符串中的每个字符都映射到该字符在字符串中的计数。我们将为此目的使用 STL unordered_map。创建频率映射后,我们将遍历第一个字符串的频率映射,对于第二个字符串中也存在的每个字符,我们将它的频率添加到我们的总和中。
示例
以下是实现上述方法的程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Function to find the sum of frequencies of str 1 and str 2
int sumOfFrequencies(char* str1, char* str2) {
int freq1[256] = {0};
int freq2[256] = {0};
for (int i = 0; i < strlen(str1); i++) {
freq1[str1[i]]++;
}
// Traverse the string str2
for (int i = 0; i < strlen(str2); i++) {
freq2[str2[i]]++;
}
int sum = 0;
for (int i = 0; i < 256; i++) {
if (freq1[i] != 0 && freq2[i] != 0) {
sum += freq1[i];
}
}
return sum;
}
// Driver code
int main() {
// strings
char str1[] = "hello";
char str2[] = "world";
// print the Output
printf("The sum of frequencies is: %d\n", sumOfFrequencies(str1, str2));
return 0;
}
输出
The sum of frequencies is: 3
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int sumOfFrequencies(string str1, string str2) {
unordered_map<char, int> freq1, freq2;
for (char c : str1) {
freq1[c]++;
}
for (char c : str2) {
freq2[c]++;
}
int sum = 0;
for (auto& kv : freq1) {
if (freq2.count(kv.first)) {
sum += kv.second;
}
}
return sum;
}
int main() {
string str1 = "hello", str2 = "world";
cout << "The sum of frequencies is: " << sumOfFrequencies(str1, str2);
return 0;
}
输出
The sum of frequencies is: 3
import java.util.HashMap;
public class Main {
// Function to find the sum of frequencies of str 1 and str 2
public static int sumOfFrequencies(String str1, String str2) {
HashMap<Character, Integer> freq1 = new HashMap<>();
HashMap<Character, Integer> freq2 = new HashMap<>();
// Inserting all the characters of string str1 in the set
for (char c : str1.toCharArray()) {
freq1.put(c, freq1.getOrDefault(c, 0) + 1);
}
// Traverse the string str2
for (char c : str2.toCharArray()) {
freq2.put(c, freq2.getOrDefault(c, 0) + 1);
}
int sum = 0;
for (char key : freq1.keySet()) {
if (freq2.containsKey(key)) {
sum += freq1.get(key);
}
}
return sum;
}
public static void main(String[] args) {
// Strings
String str1 = "hello";
String str2 = "world";
//Print the Output
System.out.println("The sum of frequencies is: " + sumOfFrequencies(str1, str2));
}
}
输出
The sum of frequencies is: 3
# Function to find the sum of frequencies of str 1 and str 2
def sum_of_frequencies(str1, str2):
freq1 = {}
freq2 = {}
# Inserting all the characters of string str1 in the set
for c in str1:
freq1[c] = freq1.get(c, 0) + 1
# Traverse the string str2
for c in str2:
freq2[c] = freq2.get(c, 0) + 1
total_sum = 0
for key, value in freq1.items():
if key in freq2:
# Increment count by 1
total_sum += value
return total_sum
# Srings
str1 = "hello"
str2 = "world"
# printing the Output
print("The sum of frequencies is:", sum_of_frequencies(str1, str2))
输出
The sum of frequencies is: 3
带测试用例的解释
让我们考虑字符串“hello”和“world”。
当我们将这些字符串传递给 sumOfFrequencies 函数时,它首先为这两个字符串创建频率映射。“hello”的频率映射为 {'h':1, 'e':1, 'l':2, 'o':1},而“world”的频率映射为 {'w':1, 'o':1, 'r':1, 'l':1, 'd':1}。
然后,该函数遍历“hello”的频率映射,对于“world”中也存在的每个字符,它将它的频率添加到总和中。公共字符是 'o' 和 'l',它们在“hello”中的频率分别为 1 和 2。因此,频率之和为 3。
因此,此程序的输出将是“频率之和为:3”。
结论
此问题提供了一个极好的机会来理解和练习各种编程语言中的频率映射概念。这是一个提高您的编码技能并了解如何处理字符串和映射以解决问题的极佳问题。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP