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.
funcalternate(sstring)int32{// Write your code herepossibilityMap:=make(map[string]bool)possibilityComb:=[][]string{}fori:=0;i<=len(s)-1;i++{forj:=0;j<=len(s)-1;j++{ifs[i]==s[j]{continue}current:=string(s[i])+string(s[j])if!possibilityMap[current]{alter:=string(s[j])+string(s[i])if!possibilityMap[alter]{possibilityMap[current]=truetemp:=[]string{}temp=append(temp,string(s[i]))temp=append(temp,string(s[j]))possibilityComb=append(possibilityComb,temp)}}}}counter:=0isRepeat:=falsefori:=0;i<len(s)-1;i++{ifi+1>=len(s){break}ifs[i]==s[i+1]{isRepeat=true}}if!isRepeat&&len(possibilityMap)==2{returnint32(len(s))}for_,v:=range(possibilityComb){newS:=strings.ReplaceAll(s,string(v[0]),"")newS=strings.ReplaceAll(newS,string(v[1]),"")newSS:=sfor_,v2:=range(newS){newSS=strings.ReplaceAll(newSS,string(v2),"")}isRepeat:=falsefori:=0;i<len(newSS)-1;i++{ifi+1>=len(newSS){break}ifnewSS[i]==newSS[i+1]{isRepeat=true}}if!isRepeat&&len(newSS)>counter{counter=len(newSS)}}returnint32(counter)}
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 →
Here is my Golang solution