You are viewing a single comment's thread. Return to all comments →
public static int numberNeeded(String first, String second) { HashMap<Character, Integer> hm=new HashMap<>(); int count=0; for(int i=0;i<first.length();i++){ if(!hm.containsKey(first.charAt(i))){ hm.put(first.charAt(i), 1); }else{ hm.put(first.charAt(i),hm.get(first.charAt(i))+1); } } for(int i=0;i<second.length();i++){ if(!hm.containsKey(second.charAt(i))){ count++; }else{ hm.put(second.charAt(i),hm.get(second.charAt(i))-1); } } for(Entry<Character,Integer> entry:hm.entrySet()){ if(entry.getValue()!=0){ count+=Math.abs(entry.getValue()); } } return count; }
Have a look at this implementation of Map.
Seems like cookies are disabled on this browser, please enable them to open this website
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
Have a look at this implementation of Map.