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 and , as well as .
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 n, string s){long long alpha[26] = {0}, ans = 0;for (long long i = 0; i < n; i++){long long c = 0;for (long long j = i + 1; j < n; j++){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(n, s);return 0;}
No comments:
Post a Comment