519D. A and B and Interesting Substrings - Codeforces Solution C++

  Problem Link : 519D. A and B and Interesting Substrings 


✅ C++ Solution :

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

#define ll long long int

struct node{

	int arr[26];
	node(){
	memset(arr,0,sizeof(arr));
	}
};

int main()
{
	int x[27];
	for(int i=0;i<26;i++)
		cin>>x[i];
	string s;
	cin>>s;
	unordered_map<ll,node> mp;
	ll curr=0;
	string t;
	ll cnt=0;
	for(int i=0;i<s.length();i++)
	{
		//cout<<curr<<endl;
		if(mp.find(curr)!=mp.end())
		{
		  //  cout<<i<<" "<<curr<<endl;
			cnt+=mp[curr].arr[s[i]-'a'];
		}
		curr+=x[s[i]-'a'];
		mp[curr].arr[s[i]-'a']++;
	}

	cout<<cnt;
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!