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.
importjava.io.*;importjava.math.*;importjava.security.*;importjava.text.*;importjava.util.*;importjava.util.concurrent.*;importjava.util.regex.*;/** * Sherlock and Anagrams * Taushree Sarkar */publicclassSolution{publicstaticfinalintALPHABET_CNT=26;staticbooleanisAnagrams(Strings1,Strings2){char[]chCnt1=newchar[ALPHABET_CNT];char[]chCnt2=newchar[ALPHABET_CNT];for(inti=0,n=s1.length();i<n;i++){chCnt1[s1.charAt(i)-97]+=1;chCnt2[s2.charAt(i)-97]+=1;}for(inti=0;i<ALPHABET_CNT;i++){if(chCnt1[i]!=chCnt2[i]){returnfalse;}}returntrue;}staticintsherlockAndAnagrams(Strings){intcnt=0;for(inti=1,n=s.length();i<n;i++){List<String>subsetList=newArrayList<>();for(intj=0;j<n;j++){if(i+j<=n){subsetList.add(s.substring(j,i+j));}}for(intk=0,size=subsetList.size();k<size;k++){for(intl=k+1;l<size;l++){if(isAnagrams(subsetList.get(k),subsetList.get(l))){cnt++;}}}}returncnt;}privatestaticfinalScannerscanner=newScanner(System.in);publicstaticvoidmain(String[]args)throwsIOException{BufferedWriterbufferedWriter=newBufferedWriter(newFileWriter(System.getenv("OUTPUT_PATH")));intq=scanner.nextInt();scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");for(intqItr=0;qItr<q;qItr++){Strings=scanner.nextLine();intresult=sherlockAndAnagrams(s);bufferedWriter.write(String.valueOf(result));bufferedWriter.newLine();}bufferedWriter.close();scanner.close();}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sherlock and Anagrams
You are viewing a single comment's thread. Return to all comments →
java8