执行给定操作后查找出现次数最多的字符
在本文中,我们将探讨在执行给定的一组操作后查找字符串中出现次数最多的字符的概念。此问题通常出现在编程挑战和面试中,掌握解决方案有助于增强您的字符串操作和算法技能。我们将解释问题陈述,讨论使用的算法,提供实现,并提供一个测试用例示例来演示解决方案。
问题陈述
给定一个字符串 s 和一组操作,在执行所有操作后查找出现次数最多的字符。每个操作都由一对 (i, j) 组成,表示我们应该交换字符串中位置 i 和 j 处的字符。
算法
创建一个频率数组来存储字符串中每个字符的出现次数。
遍历操作,交换指定位置的字符。
每次交换后更新频率数组。
遍历频率数组以查找出现次数最多的字符。
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
实现
示例
以下是上述算法的程序:
#include <stdio.h> #include <string.h> char maxOccurringChar(const char *s, const int operations[][2], int numOperations) { int frequency[26] = {0}; char modifiedString[100]; strcpy(modifiedString, s); // Initialize the frequency array with the original string's character occurrences for (int i = 0; modifiedString[i] != '\0'; i++) { frequency[modifiedString[i] - 'a']++; } for (int op = 0; op < numOperations; op++) { int i = operations[op][0]; int j = operations[op][1]; // Decrement the frequency of the characters being swapped frequency[modifiedString[i] - 'a']--; frequency[modifiedString[j] - 'a']--; // Perform the swap char temp = modifiedString[i]; modifiedString[i] = modifiedString[j]; modifiedString[j] = temp; // Increment the frequency of the swapped characters frequency[modifiedString[i] - 'a']++; frequency[modifiedString[j] - 'a']++; } // Find the character with the maximum occurrence int maxFrequency = 0; char maxChar = 'a'; for (int i = 0; i < 26; i++) { if (frequency[i] > maxFrequency) { maxFrequency = frequency[i]; maxChar = 'a' + i; } } return maxChar; } int main() { const char *s = "aabcbdb"; int operations[][2] = { {1, 4}, {2, 5} }; int numOperations = sizeof(operations) / sizeof(operations[0]); char maxChar = maxOccurringChar(s, operations, numOperations); printf("The maximum occurring character after performing the operations is: %c\n", maxChar); return 0; }
输出
The maximum occurring character after performing the operations is: b
#include <iostream> #include <string> #include <vector> #include <algorithm> char maxOccurringChar(const std::string &s, const std::vector<std::pair<int, int>> &operations) { std::vector<int> frequency(26, 0); std::string modifiedString = s; // Initialize the frequency array with the original string's character occurrences for (char c : modifiedString) { frequency[c - 'a']++; } for (const auto &op : operations) { int i = op.first; int j = op.second; // Decrement the frequency of the characters being swapped frequency[modifiedString[i] - 'a']--; frequency[modifiedString[j] - 'a']--; // Perform the swap std::swap(modifiedString[i], modifiedString[j]); // Increment the frequency of the swapped characters frequency[modifiedString[i] - 'a']++; frequency[modifiedString[j] - 'a']++; } // Find the character with the maximum occurrence int maxFrequency = 0; char maxChar = 'a'; for (int i = 0; i < 26; i++) { if (frequency[i] > maxFrequency) { maxFrequency = frequency[i]; maxChar = 'a' + i; } } return maxChar; } int main() { std::string s = "aabcbdb"; std::vector<std::pair<int, int>> operations = { {1, 4}, {2, 5} }; char maxChar = maxOccurringChar(s, operations); std::cout << "The maximum occurring character after performing the operations is: " << maxChar << std::endl; return 0; }
输出
The maximum occurring character after performing the operations is: b
import java.util.Arrays; public class MaxOccurringChar { public static char maxOccurringChar(String s, int[][] operations) { int[] frequency = new int[26]; char[] modifiedString = s.toCharArray(); // Initialize the frequency array with the original string's character occurrences for (char c : modifiedString) { frequency[c - 'a']++; } for (int[] op : operations) { int i = op[0]; int j = op[1]; // Decrement the frequency of the characters being swapped frequency[modifiedString[i] - 'a']--; frequency[modifiedString[j] - 'a']--; // Perform the swap char temp = modifiedString[i]; modifiedString[i] = modifiedString[j]; modifiedString[j] = temp; // Increment the frequency of the swapped characters frequency[modifiedString[i] - 'a']++; frequency[modifiedString[j] - 'a']++; } // Find the character with the maximum occurrence int maxFrequency = 0; char maxChar = 'a'; for (int i = 0; i < 26; i++) { if (frequency[i] > maxFrequency) { maxFrequency = frequency[i]; maxChar = (char) ('a' + i); } } return maxChar; } public static void main(String[] args) { String s = "aabcbdb"; int[][] operations = { {1, 4}, {2, 5} }; char maxChar = maxOccurringChar(s, operations); System.out.println("The maximum occurring character after performing the operations is: " + maxChar); } }
输出
The maximum occurring character after performing the operations is: b
def max_occuring_char(s, operations): frequency = [0] * 26 modified_string = list(s) # Initialize the frequency array with the original string's character occurrences for c in modified_string: frequency[ord(c) - ord('a')] += 1 for op in operations: i, j = op # Decrement the frequency of the characters being swapped frequency[ord(modified_string[i]) - ord('a')] -= 1 frequency[ord(modified_string[j]) - ord('a')] -= 1 # Perform the swap modified_string[i], modified_string[j] = modified_string[j], modified_string[i] # Increment the frequency of the swapped characters frequency[ord(modified_string[i]) - ord('a')] += 1 frequency[ord(modified_string[j]) - ord('a')] += 1 # Find the character with the maximum occurrence max_frequency = 0 max_char = 'a' for i in range(26): if frequency[i] > max_frequency: max_frequency = frequency[i] max_char = chr(ord('a') + i) return max_char s = "aabcbdb" operations = [(1, 4), (2, 5)] max_char = max_occuring_char(s, operations) print("The maximum occurring character after performing the operations is:", max_char)
输出
The maximum occurring character after performing the operations is: b
测试用例示例
让我们考虑以下示例:
字符串:“aabcbdb”
操作:{ {1, 4}, {2, 5} }
执行第一个操作 (1, 4):“abacbdb”
执行第二个操作 (2, 5):“abcabdb”
执行完操作后,字符串变为“abcabdb”。修改后的字符串中出现次数最多的字符是 'b',出现了三次。
结论
在本文中,我们探讨了在执行给定的一组操作后查找字符串中出现次数最多的字符的问题。我们讨论了算法,提供了正确的实现,并提供了一个测试用例示例来演示解决方案。掌握此类问题有助于增强您的字符串操作和算法技能,这些技能对于编程挑战和面试至关重要。请记住,根据需要仔细初始化和更新频率数组以确保结果准确。
广告