Home > Backend Development > C++ > Find the character that occurs most often after performing a given operation

Find the character that occurs most often after performing a given operation

WBOY
Release: 2023-08-27 09:49:06
forward
910 people have browsed it

Find the character that occurs most often after performing a given operation

In this article, we will explore the concept of finding the most frequent characters in a string after performing a given set of operations. This question often comes up in programming challenges and interviews, and having the solution can help strengthen your string manipulation and algorithmic skills. We will explain the problem statement, discuss the algorithm used, show the C implementation, and provide test case examples to demonstrate the solution.

Problem Statement

Given a string s and a set of operations, find the largest occurrence of characters after performing all operations. Each operation consists of a pair (i, j), which means that we want to exchange the characters at positions i and j in the string.

algorithm

  • Create a frequency array to store the number of occurrences of each character in a string.

  • Iterative operation, exchange characters at the specified position.

  • Update the frequency array after each exchange.

  • Iterate over the frequency array to find the character that appears the most.

C implementation

Example

#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;
}
Copy after login

Output

The maximum occurring character after performing the operations is: b
Copy after login

Test case example

Let us consider the following example -

  • String: "aabcbdb"

  • Operation: { {1, 4}, {2, 5} }

  • Perform the first operation (1, 4): "abacbdb"

  • Perform the second operation (2, 5): "abcabdb"

After performing the operation, the string becomes "abcabdb". The most common character in the modified string is 'b', which appears three times.

in conclusion

In this article, we explore the problem of finding the most frequent characters in a string after performing a given set of operations. We discuss the algorithm, propose a revised C implementation, and provide an example test case to demonstrate the solution. Mastering questions like these can help strengthen your string manipulation and algorithmic skills, which are crucial for programming challenges and interviews. Remember to carefully initialize and update the frequency array as needed to ensure accurate results.

The above is the detailed content of Find the character that occurs most often after performing a given operation. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template