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.*;publicclassSolution{// Complete the sherlockAndAnagrams function below.staticintsherlockAndAnagrams(Strings){Map<String,Integer>al=newHashMap<>();intc=0;for(intx=0;x<s.length();x++){for(inty=0+x;y<s.length();y++){charar[]=(s.substring(x,y+1)).toCharArray();Arrays.sort(ar);Stringq=(String.valueOf(ar));Integero=al.get(q);if(o==null){al.put(q,1);}else{al.put(q,o+1);}}}for(Stringas:al.keySet()){intww=al.get(as);c+=ww*(ww-1)/2;}returnc;}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 →