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.
I also implemented it with a hashmap but only used one.
My solution was very similar to clinton_stamper@ approach.
First I converted the longer string to a
HashMap<Character,Integer>
.
Then I iterated over the second string. In the for-loop, if the currentCharacter was in the map, then I decremented the value in the map by 1. When the value of a character reached 0, it meant that the longer string no longer had this character so I removed it from the map. For all characters that I came across without an entry in the map, I added 1 to the global differenceCount.
Then all I did was iterate over the entries left over in the map and add the values to the differenceCount.
Below is the code:
publicclassSolution{publicstaticintnumberNeeded(Stringfirst,Stringsecond){Map<Character,Integer>mapOfCharacterCount;if(string1.length()>=string2.length()){mapOfCharacterCount=getMapOfCharacterCount(string1);returngetCharacterDifferenceBetweenStringAndMap(string2,mapOfCharacterCount);}else{mapOfCharacterCount=getMapOfCharacterCount(string2);returngetCharacterDifferenceBetweenStringAndMap(string1,mapOfCharacterCount);}}publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);Stringa=in.next();Stringb=in.next();System.out.println(numberNeeded(a,b));}privatestaticMap<Character,Integer>getMapOfCharacterCount(finalStringstring){finalMap<Character,Integer>mapOfCharacterCount=newHashMap<Character,Integer>();for(IntegercurrentIndex=0;currentIndex<string.length();currentIndex++){CharactercurrentCharacter=(Character)string.charAt(currentIndex);if(mapOfCharacterCount.containsKey(currentCharacter)){IntegercharacterCount=mapOfCharacterCount.get(currentCharacter);mapOfCharacterCount.put(currentCharacter,++characterCount);}else{mapOfCharacterCount.put(currentCharacter,1);}}returnmapOfCharacterCount;}privatestaticintgetCharacterDifferenceBetweenStringAndMap(finalStringstring,finalMap<Character,Integer>mapOfCharacterCountForSecondString){intdifferenceCount=0;for(inti=0;i<string.length();i++){CharactercurrentCharacter=string.charAt(i);if(mapOfCharacterCountForSecondString.containsKey(currentCharacter)){IntegercharacterCount=mapOfCharacterCountForSecondString.get(currentCharacter);if((characterCount-1)==0){// The count for this character has reached 0 in the second string. Remove it from the map// so that if the character comes up again in the first string, we know to add to the // difference count.mapOfCharacterCountForSecondString.remove(currentCharacter);}else{mapOfCharacterCountForSecondString.put(currentCharacter,--characterCount);}}else{differenceCount++;}}for(Map.Entry<Character,Integer>entry:mapOfCharacterCountForSecondString.entrySet()){differenceCount+=entry.getValue();}returndifferenceCount;}}
Strings: Making Anagrams
You are viewing a single comment's thread. Return to all comments →
I also implemented it with a hashmap but only used one.
My solution was very similar to clinton_stamper@ approach.
First I converted the longer string to a
.
Then I iterated over the second string. In the for-loop, if the currentCharacter was in the map, then I decremented the value in the map by 1. When the value of a character reached 0, it meant that the longer string no longer had this character so I removed it from the map. For all characters that I came across without an entry in the map, I added 1 to the global differenceCount.
Then all I did was iterate over the entries left over in the map and add the values to the differenceCount.
Below is the code: