Holiday Season

It's a holiday season for all school students around the world! Unfortunately, Mahamba is busy preparing for International Olympiad in Informatics, which will be held in Tehran, Iran. He is now facing a new challenge from his teacher Aceka, and it goes something like this:

You have a string x of length N, which consists of small English letters. You have to find the number of indexes a, b, c, and d, such that 1<=a<b<c<d<=N and xa==xc, as well as xb==xd.

He is baffled and definitely needs some help. So, you, the best programmer in Lalalandia, decided to give him a hand!

Example:

Input:  n = 5, s = "ababa"
Output: 2

Approach

C++

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

void holidaySeason(long long nstring s)
{
    long long alpha[26] = {0}, ans = 0;
    for (long long i = 0i < ni++)
    {
        long long c = 0;
        for (long long j = i + 1j < nj++)
        {
            if (s[i] == s[j])
            {
                ans += c;
            }
            c += alpha[s[j] - 'a'];
        }
        alpha[s[i] - 'a']++;
    }
    cout << ans << "\n";
}
int main()
{
    long long n = 5;
    string s = "ababa";

    holidaySeason(ns);
    return 0;
}


No comments:

Post a Comment