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 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.

Implementation

Example

Here're the programs to the above algorithm −

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

Output

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

Output

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

Output

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)

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 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: 20-Oct-2023

437 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements