String Clash

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.

Example:

Input:  s = "aab", t = "cba"
Output: 5

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int stringClash(string sstring t)
{

    int n = s.size();
    int m = t.size();
    int f[26] = {0};
    for (int i = 0i < ni++)
        f[s[i] - 'a']++;
    for (int i = 0i < mi++)
        f[t[i] - 'a']++;
    int cnt = 0flag = 0;
    for (int i = 0i < 26i++)
    {
        if (f[i] % 2 == 0)
            cnt += f[i];
        else
        {
            if (f[i] > 1)
                cnt += f[i] - 1;
            flag = 1;
        }
    }
    if (flag == 1)
        cnt += 1;
    return cnt;
}
int main()
{
    string s = "aab"t = "cba";

    cout << stringClash(st<< "\n";

    return 0;
}


No comments:

Post a Comment