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.
funcreadAndSearchForAnagrams(){// Read StringsletfstString=readLine()!letsndString=readLine()!letcaractersCount=charactersToDeleteToBeAnagrams(from:fstString,with:sndString)print(caractersCount)}funccharactersToDeleteToBeAnagrams(fromfirstString:String,withsecondString:String)->Int{//Mutable copy of the second StringvarstringTwo=secondStringvarcounter=0// Search if each character of first string exist in second StringforindexinfirstString.characters.indices{ifletindexInStringTwo=stringTwo.characters.index(of:firstString[index]){// If exist, then increment the counter by 2(one per character in each string)counter+=2// And remove it from second stringstringTwo.remove(at:indexInStringTwo)}}// Concatenate them and return the amount of characters minus the counter of characters that exist on both.// That give us the numer of characters that doesn't exist on bothletcompleteString=firstString+secondStringreturncompleteString.characters.count-counter}readAndSearchForAnagrams()
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 →
I solved similarly in Swift 3: