You are viewing a single comment's thread. Return to all comments →
Java Code
public static int sherlockAndAnagrams(String s) { int frame = 1; int result = 0; int len = s.length(); while(frame < len){ for(int i = 0; i <= len - frame; i++){ String curr = s.substring(i, i + frame); for(int j = i + 1; j <= len - frame; j++){ Map<Character, Long> charCountMap = s.substring(j, j + frame).chars().mapToObj(c -> (char) c).collect(Collectors.groupingBy(c -> c, Collectors.counting())); if(itContains(charCountMap, curr)){ ++result; } } } ++frame; } return result; } private static boolean itContains(Map<Character, Long> l, String c){ for(Character cr : c.toCharArray()) { long v = l.getOrDefault(cr, 0L); if(v == 0) { return false; } l.put(cr, --v); } return true; }
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 →
Java Code