Addition ain't simple

Jack is awesome. His friends call him little Einstein. To test him, his friends gave him a string. They told him to add the string with its reverse string and follow these rules:

1. Every ith character of the string will be added to every ith character of the reverse string.

2. Both strings will contain only lower case alphabets(a-z).

3.Eg:- a+a=b,a+c=d,z+a=a (Refer to sample test cases for more details)

Example:

Input:  s = "hello"
Output: wqxqw

Approach

 C++

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

string additionAintSimple(string s)
{

    map<charintmp;
    for (int j = 0j < 26j++)
        mp['a' + j] = j + 1;

    map<intcharmp1;
    for (int i = 0i < 26i++)
        mp1[i + 1] = 'a' + i;
    string y = s;

    reverse(s.begin(), s.end());
    string str = "";

    for (int j = 0j < s.size(); j++)
    {

        int x = mp[s[j]] + mp[y[j]];
        x = x % 26;
        if (x == 0)
            str += 'z';
        else
            str += mp1[x];
    }
    return str;
}
int main()
{

    string s = "hello";

    cout << additionAintSimple(s<< "\n";

    return 0;
}


No comments:

Post a Comment