# Check if all strings of an array can be made same by interchanging characters

In this article, we will explore the problem of checking whether all strings of an array can be made the same by interchanging characters. We will first understand the problem statement and then investigate both the naive and efficient approaches to solve this problem, along with their respective algorithms and time complexities. Lastly, we will implement the solution in C++.

## Problem Statement

Given an array of strings, determine if all strings can be made the same by interchanging characters.

## Naive Approach

The naive approach is to sort the characters of each string in the array and then compare each sorted string with the next sorted string. If all sorted strings are equal, it means that all strings can be made the same by interchanging characters.

## Algorithm (Naive)

• Sort the characters of each string in the array.

• Compare each sorted string with the next sorted string.

• If all sorted strings are equal, return true; otherwise, return false.

## C++ Code (Naive)

### Example

#include <iostream>
#include <vector>
#include <algorithm>

for (auto &str : strArray) {
std::sort(str.begin(), str.end());
}

for (size_t i = 1; i < strArray.size(); i++) {
if (strArray[i - 1] != strArray[i]) {
return false;
}
}

return true;
}

int main() {
std::vector<std::string> strArray = {"abb", "bba", "bab"};

std::cout << "All strings can be made the same by interchanging characters." << std::endl;
} else {
std::cout << "All strings cannot be made the same by interchanging characters." << std::endl;
}

return 0;
}


### Output

All strings can be made the same by interchanging characters.


Time Complexity (Naive): O(n * m * log(m)), where n is the number of strings in the array and m is the maximum length of a string in the array.

## Efficient Approach

The efficient approach is to count the frequency of each character in each string and store the counts in a frequency array. Then, compare the frequency arrays of all strings. If they are equal, it means that all strings can be made the same by interchanging characters.

## Algorithm (Efficient)

• Initialize a vector of frequency arrays for each string in the array.

• Count the frequency of each character in each string and store it in the corresponding frequency array.

• Compare the frequency arrays of all strings.

• If all frequency arrays are equal, return true; otherwise, return false.

## C++ Code (Efficient)

### Example

#include <iostream>
#include <vector>
#include <algorithm>

std::vector<std::vector<int>> freqArrays(strArray.size(), std::vector<int>(26, 0));

for (size_t i = 0; i < strArray.size(); i++) {
for (char ch : strArray[i]) {
freqArrays[i][ch - 'a']++;
}
}

for (size_t i = 1; i < freqArrays.size(); i++) {
if (freqArrays[i - 1] != freqArrays[i])
return false;
}

return true;
}

int main() {
std::vector<std::string> strArray = {"abb", "bba", "bab"};
std::cout << "All strings can be made the same by interchanging characters." << std::endl;
} else {
std::cout << "All strings cannot be made the same by interchanging characters." << std::endl;
}

return 0;
}


### Output

All strings can be made the same by interchanging characters.


Time Complexity (Efficient) − O(n * m), where n is the number of strings in the array and m is the maximum length of a string in the array.

## Conclusion

In this article, we explored the problem of checking whether all strings of an array can be made the same by interchanging characters. We discussed both the naive and efficient approaches to solve this problem, along with their algorithms and time complexities. The efficient approach, which uses frequency arrays to compare characters' occurrences, provides a significant improvement in time complexity over the naive approach.

Updated on: 17-May-2023

52 Views 