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
- Anagram
- Discussions
Anagram
Anagram
Sort by
recency
|
960 Discussions
|
Please Login in order to post a comment
My c++ solution using map, here is the explanation : https://youtu.be/0-xHzWDVAME
include
using namespace std;
int test(string s) { if (s.length() % 2 == 1) return -1; int n = s.length(); map mp; for (int i = 0; i < n / 2; i++) mp[s[i]]++; for (int i = n / 2; i < n; i++) if (mp[s[i]] > 0) mp[s[i]]--; int dem = 0; for (auto it : mp) dem += it.second; return dem; }
int main() { int t; cin >> t; while (t--) { string s; cin >> s; cout << test(s) << endl; } return 0; }
Java 8 This is one of the easiest way to solve this problem.
for Python3 Platform