Amongst the lockdown and all the hassles, there is a Professor – Professor Oak, who spends his time watching stars in the sky.
One day Professor Oak saw alphabets appearing on the stars. He was so fascinated by them that he started making a list of numbers and for every new star, he inserted a number X into the list. This X is equal to the no. of times the alphabet on the current star has previously appeared.
After seeing N stars, he decided to stop and calculate the sum of all the N numbers.
Professor Oak has spent all his life watching stars and does not know how to add.
Finding the sum.
Example:
Input: n = 5, s="abaab"
Output: 4
Approach
C++
#include <bits/stdc++.h>using namespace std;long long starStuddedLockdown(int n, string s){long long arr[n] = {0};map<char, long long> mp;for (long long i = 0; i < n; i++){if (mp.find(s[i]) == mp.end())arr[i] = 0;elsearr[i] = mp[s[i]];mp[s[i]]++;}long long sum = 0;for (long long i = 0; i < n; i++)sum += arr[i];return sum;}int main(){long long n = 5;string s = "abaab";cout << starStuddedLockdown(n, s);return 0;}
No comments:
Post a Comment