We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Making Anagrams
- Discussions
Making Anagrams
Making Anagrams
+ 0 comments Here is my c++ solution, you can watch the explanation here : https://youtu.be/B4pZbX0VzzU
#include <bits/stdc++.h> using namespace std; int main() { string fstr, sstr; cin >> fstr; cin >> sstr; vector<int>fsc(26,0), ssc(26,0); int i, result = 0; for(i = 0; i < fstr.size(); i++) fsc[fstr[i] - 'a']++; for(i = 0; i < sstr.size(); i++) ssc[sstr[i] - 'a']++; for(i = 0; i < 26; i++) result += abs(fsc[i] - ssc[i]); cout << result << endl; return 0; }
+ 0 comments int makingAnagrams(string s1, string s2) { int arr[26] = {0}; for(int i = 0 ; i < s1.size(); i++) { arr[s1[i] - 'a']++; }
for(int i = 0 ; i < s2.size(); i++) { arr[s2[i] - 'a']--; } int count = 0; for(int i = 0; i < 26; i++) { if(arr[i] > 0) { count = count + arr[i]; } else if(arr[i] < 0) { count = count + abs(arr[i]); } } return count;
}
return 0;
}
+ 0 comments int makingAnagrams(string s1, string s2) { unordered_map<char, int> mymap1; int i = 0, remove = 0; while(i < s1.size()) { mymap1[s1[i]] += 1; i++; } i = 0; while(i < s2.size()) { mymap1[s2[i]] -= 1; i++; } auto it = mymap1.begin(); while(it != mymap1.end()) { remove += abs(it->second); it++; } return remove; }
+ 0 comments Using Python3 defaultdict
def makingAnagrams(s1, s2): d=defaultdict(int) for c in s1: d[c]+=1 for c in s2: d[c]-=1 return sum(abs(v) for k,v in d.items())
+ 0 comments C++:
int makingAnagrams(string s1, string s2) { for(int i = 0; i < s1.size(); i++) for(int j = 0; j < s2.size(); j++) if(s1[i] == s2[j]){ s1.erase(i, 1); s2.erase(j, 1); i--; j--; } return s1.size() + s2.size(); }
Load more conversations
Sort 689 Discussions, By:
Please Login in order to post a comment