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.
publicstaticintalternate(Strings){// Write your code hereSet<Character>ts=newTreeSet<>();List<Character>al=newArrayList<>();intlen=0;for(Characterc:s.toCharArray())ts.add(c);for(Characterc:ts)al.add(c);for(inti=0;i<al.size()-1;i++){for(intj=1;j<al.size();j++){StringBuffersb1=newStringBuffer(s);StringBuffersb2=newStringBuffer();for(Characterc:sb1.toString().toCharArray()){if(c==al.get(i)||c==al.get(j)){sb2.append(c);}}//System.out.println(sb2.toString());Characterc1=sb2.charAt(0);Characterc2=null;if(sb2.length()>1)c2=sb2.charAt(1);intmax=0;if(c1!=c2){for(intk=0;k<sb2.length();k++){if(k%2==0&&sb2.charAt(k)!=c1){max=0;break;}elseif(k%2!=0&&sb2.charAt(k)!=c2){max=0;break;}elsemax++;}}if(max>len)len=max;}}returnlen;}
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 →
Java Code: