字串中存在的另一個字串字元頻率之和


在本文中,我們將探討一個與使用各種程式語言進行字串操作相關的有趣問題。問題陳述是“字串中存在的另一個字串字元頻率之和”。此問題提供了一個極好的機會來增強您對字串操作、字元頻率計算以及 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”。

結論

此問題提供了一個極好的機會來理解和練習各種程式語言中的頻率對映概念。這是一個提高您的編碼技能並瞭解如何處理字串和對映以解決問題的極佳問題。

更新於: 2023年10月27日

578 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告