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 'alternate' function below.## The function is expected to return an INTEGER.# The function accepts STRING s as parameter.#defconsecutive_chars(s):foriinrange(len(s)-1):ifs[i]==s[i+1]:return0returnlen(s)defalternate(s):# Write your code heretmp_list=list(set(s))iflen(tmp_list)<2:return0tmp_list=sorted(tmp_list)length_list=[]foriinrange(len(tmp_list)-1):forjinrange(i+1,len(tmp_list)):new_s=[charforcharinsifchar==tmp_list[i]orchar==tmp_list[j]]length_list.append(consecutive_chars(new_s))returnmax(length_list)if__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')l=int(input().strip())s=input()result=alternate(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
Two Characters
You are viewing a single comment's thread. Return to all comments →