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.
makeAnagram<-function(a,b){# initialize removalsremovals<-0# convert inputs to frequency tablesa<-table(unlist(strsplit(a,"")[[1]]))b<-table(unlist(strsplit(b,"")[[1]]))# iterate through elements that exist in both inputsfor(letterinintersect(names(a),names(b))){# add difference in letter count to removalsremovals<-removals+abs(a[letter]-b[letter])}# remove tallies that exist in only one inputremovals<-removals+sum(a[!(names(a)%in%names(b))],na.rm=T)removals<-removals+sum(b[!(names(b)%in%names(a))],na.rm=T)return(removals)}
Cookie support is required to access HackerRank
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 →
R