Pikachu and the Game of Strings

Pikachu has recently learned a new move s. He knows he can work hard and convert it into a stronger move 

t. Both the moves s and t contain the same number of letters.

In a single day, Pikachu can increase any letter of move s by one, that is, in a single day, he can convert letter A to BC to DM to N and so on. He can also convert letter Z to letter A

Pikachu just realized he also has a hidden ability. It can help him increase any letter of move s by 13,  that is, in a single day, he can convert letter A to letter NB into OM into Z , O into B and so on.  

Now Pikachu wants to know the minimum number of days in which he can convert the move s into move t  ?

Example:

Input:  n = 4, s = "ABCT", t ="PBDI"
Output: 7

Approach

C++

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

long long gameOfString(long long nstring sstring t)
{
    map<charlong longmp;
    for (long long i = 0i < 26i++)
        mp[i + 'A'] = i + 1;
    long long ans = 0;
    for (long long i = 0i < ni++)
    {
        if (s[i] != t[i])
        {
            long long x = mp[s[i]]y = mp[t[i]];

            if (y > x)
            {
                long long z = 13 + x;
                if (z > y)
                    ans += y - x;
                else
                {
                    ans += 1;
                    ans += y - z;
                }
            }
            else
            {
                long long z = x + 13;
                if (z >= 26)
                {
                    z = z % 26;
                    if (z > y)
                        ans += 26 - x + y;
                    else
                    {
                        ans += 1;
                        ans += y - z;
                    }
                }
                else
                {
                    ans += 1;
                    ans += 26 - z + y;
                }
            }
        }
    }
    return ans;
}
int main()
{

    long long n = 4;

    string s = "ABCT"t = "PBDI";

    cout << gameOfString(nst<< "\n";

    return 0;
}


No comments:

Post a Comment