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.
classResult{/* * Complete the 'alternate' function below. * * The function is expected to return an INTEGER. * The function accepts STRING s as parameter. */publicstaticintalternate(Strings){// Write your code hereint[][]countMap=newint[26][26];char[][]lastChar=newchar[26][26];intans=0;for(inti=0;i<s.length();i++){charc=s.charAt(i);intindex=c-'a';for(intj=0;j<26;j++){if(countMap[index][j]==-1){continue;}if(lastChar[index][j]==c){countMap[index][j]=-1;countMap[j][index]=-1;}else{lastChar[index][j]=c;lastChar[j][index]=c;countMap[index][j]+=1;countMap[j][index]+=1;}}}for(inti=0;i<26;i++){for(intj=0;j<26;j++){if(i==j){continue;}ans=Math.max(countMap[i][j],ans);}}if(s.length()==1){return0;}returnans;}}
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 →