Find the maximum occurring character after performing the given operations


In this article, we will explore the concept of finding the maximum occurring character in a string after performing a given set of operations. This problem often arises in programming challenges and interviews, and mastering the solution helps strengthen your string manipulation and algorithm skills. We will explain the problem statement, discuss the algorithm used, present the C++ implementation, and provide a test case example to demonstrate the solution.

Problem Statement

Given a string s and a set of operations, find the maximum occurring character after performing all the operations. Each operation consists of a pair (i, j), representing that we should swap the characters at positions i and j in the string.

Algorithm

  • Create a frequency array to store the occurrence count of each character in the string.

  • Iterate through the operations, swapping the characters at the specified positions.

  • Update the frequency array after each swap.

  • Iterate through the frequency array to find the character with the maximum occurrence.

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;
}

Output

The maximum occurring character after performing the operations is: b

Test Case Example

Let's consider the following example −

  • String: "aabcbdb"

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

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

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

After performing the operations, the string becomes "abcabdb". The maximum occurring character in the modified string is 'b', which occurs three times.

Conclusion

In this article, we explored the problem of finding the maximum occurring character in a string after performing a given set of operations. We discussed the algorithm, presented the corrected C++ implementation, and provided a test case example to demonstrate the solution. Mastering this type of problem helps enhance your string manipulation and algorithm skills, which are essential for programming challenges and interviews. Remember to carefully initialize and update the frequency array as needed to ensure accurate results.

Updated on: 17-May-2023

377 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements