We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
#!/bin/python3importmathimportosimportrandomimportreimportsys# Complete the substrCount function below.defsubstrCount(n,s):count=n# Find places where xyx existxyx_indexes=[iforiinrange(1,n-1)ifs[i-1]==s[i+1]ands[i]!=s[i-1]]forindexinxyx_indexes:char,j=s[index-1],2whileindex-j>=0andindex+j<nands[index-j]==charands[index+j]==char:j+=1count+=(j-1)# Now, get places with consecutive charactersi=0whilei<n-1:c=1whilei+c<nands[i]==s[i+c]:c+=1ifc>1:count+=(c*(c-1)//2)i+=creturncountif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')n=int(input())s=input()result=substrCount(n,s)fptr.write(str(result)+'\n')fptr.close()
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Special String Again
You are viewing a single comment's thread. Return to all comments →
Hi all, here is the working solution in Python: