I'm trying to figure out how my code could be improved. I solved a HackerRank problem from the Cracking the Coding Interview section, and am thinking that it should be able to be solved a simpler than what I did.
The problem is as follows:
Strings: Making Anagrams
Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example,
bacdc
anddcbac
are anagrams, butbacdc
anddcbad
are not.Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings,
a
andb
, that may or may not be of the same length, determine the minimum number of character deletions required to makea
andb
anagrams. Any characters can be deleted from either of the strings.Input Format
The first line contains a single string,
a
. The second line contains a single string,b
.Constraints
- \$1 \le |a|,|b| \le 10^4\$
- It is guaranteed that
a
andb
consist of lowercase English alphabetic letters (i.e., a through z).Output Format
Pring a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.
Sample Input
cde abc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of each other:
- Remove
d
ande
fromcde
to getc
.- Remove
a
andb
fromabc
to getc
.We must delete 4 characters to make both strings anagrams, so we print 4 on a new line.
My Implementation
I am using C++, and since strings are immutable, what I decided to do was to create two int
arrays (vectors, actually) that hold the ASCII value of the chars in each string. Then I would sort the vectors. Then I would iterate through the arrays together, counting the number of elements that don't exist in the other.
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int calcDeletions(vector<int> a, vector<int> b) {
int deletions = 0;
int ai = 0;
int bi = 0;
// step through arrays until the end of one is reached
while (ai < a.size() && bi < b.size()) {
if (a[ai] < b[bi]) {
deletions++;
ai++;
}
else if (a[ai] > b[bi]) {
deletions++;
bi++;
}
else {
ai++;
bi++;
}
}
// carry over left-overs
deletions += (a.size() - ai);
deletions += (b.size() - bi);
return deletions;
}
int number_needed(string a, string b) {
vector<int> aInt;
vector<int> bInt;
// create two int vectors to store ascii val of strings
for (int i = 0; i < a.length(); i++) {
aInt.push_back((int)a.at(i));
}
for (int i = 0; i < b.length(); i++) {
bInt.push_back((int)b.at(i));
}
// sort vectors
sort(aInt.begin(), aInt.end());
sort(bInt.begin(), bInt.end());
// call calcDeletions function with the longer vector passed as first arg
if (aInt.size() > bInt.size()) {
return calcDeletions(aInt, bInt);
}
else {
return calcDeletions(bInt, aInt);
}
}
int main() {
string a;
cin >> a;
string b;
cin >> b;
cout << number_needed(a, b) << endl;
return 0;
}
What is a better, more efficient way of solving this problem?