Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth String Clash problem solution

YASH PAL, 31 July 2024
In this HackerEarth String Clash problem solution, you are given two strings S and T of equal lengths. You need to pick some characters from the first string, some from the second string, and then form a new string by rearranging the characters you have picked. You need to find the length of the maximum string that you can make which will be a palindrome.
HackerEarth String Clash problem solution

HackerEarth String Clash problem solution.

#include<bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "n"
#define eps 0.00000001
LL pow(LL a,LL b,LL m){LL x=1,y=a;while(b > 0){if(b%2 == 1){x=(x*y);if(x>m) x%=m;}y = (y*y);if(y>m) y%=m;b /= 2;}return x%m;}
LL gcd(LL a,LL b){if(b==0) return a; else return gcd(b,a%b);}
LL gen(LL start,LL end){LL diff = end-start;LL temp = rand()%start;return temp+diff;}
using namespace std;
int f[1000001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int ans = 0;
string a , b;
cin >> a >> b;
for(int i = 0; i < a.length(); i++) {
f[a[i] - 'a']++;
}
for(int i = 0; i < b.length(); i++) {
f[b[i] - 'a']++;
}
bool flag = 0;
for(int i = 0; i < 26; i++) {
ans = ans + f[i] - (f[i] % 2);
if(f[i] % 2) {
flag = 1;
}
}
cout << ans + flag;
}

Second solution

#include <bits/stdc++.h>

using namespace std;

vector<int> f(26, 0);

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s, t;
cin >> s >> t;
int n = s.size();
for(int i = 0; i < n; i ++)
f[s[i] - 'a'] ++;
for(int i = 0; i < n; i ++)
f[t[i] - 'a'] ++;
bool odd = 0;
int total_len = 0;
for(int i = 0; i < 26; i ++) {
if(f[i] & 1) {
odd = 1;
f[i] --;
}
total_len += f[i];
}
if(odd)
total_len ++;
cout << total_len;
return 0;
}
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes