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<char, int> mp;for (int j = 0; j < 26; j++)mp['a' + j] = j + 1;map<int, char> mp1;for (int i = 0; i < 26; i++)mp1[i + 1] = 'a' + i;string y = s;reverse(s.begin(), s.end());string str = "";for (int j = 0; j < s.size(); j++){int x = mp[s[j]] + mp[y[j]];x = x % 26;if (x == 0)str += 'z';elsestr += mp1[x];}return str;}int main(){string s = "hello";cout << additionAintSimple(s) << "\n";return 0;}
No comments:
Post a Comment