You are viewing a single comment's thread. Return to all comments →
public static int makingAnagrams(String s1, String s2) { Map<String, Long> s1map = Arrays.stream(s1.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); Map<String, Long> s2map = Arrays.stream(s2.split("")).collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); long delcount = 0; Set<Entry<String, Long>> s1entryset = s1map.entrySet(); Iterator<Entry<String, Long>> s1iterator = s1entryset.iterator(); while(s1iterator.hasNext()) { Entry<String, Long> entry = s1iterator.next(); String s1key = entry.getKey(); Long s1val = entry.getValue(); if(s2map.get(s1key) != null) { if(s1val > s2map.get(s1key)) delcount += s1val - s2map.get(s1key); else if(s2map.get(s1key) > s1val) delcount += s2map.get(s1key) - s1val; } else if(s2map.get(s1key) == null) { delcount += s1val; } } Set<Entry<String, Long>> s2entryset = s2map.entrySet(); Iterator<Entry<String, Long>> s2iterator = s2entryset.iterator(); while(s2iterator.hasNext()) { Entry<String, Long> entry = s2iterator.next(); String s2key = entry.getKey(); Long s2val = entry.getValue(); if(s1map.get(s2key) == null) { delcount += s2val; } } return (int)delcount; }
Seems like cookies are disabled on this browser, please enable them to open this website
Making Anagrams
You are viewing a single comment's thread. Return to all comments →