Making Anagrams

  • + 0 comments

    JS solution:

    function makeAnagram(a, b) {
      let firstArr = [...a].sort()
      let secondArr = [...b].sort()
    
      let numDel = 0
      let i = 0
    
      while (i < firstArr.length) {
        const first = firstArr[i]
        const second = secondArr[i]
        if (first !== second) {
          numDel++
          if (first > second) {
            secondArr.splice(i, 1)
          } else {
            firstArr.splice(i, 1)
          }
        } else {
          i++
        }
      }
    
      if (firstArr.length !== secondArr.length)
        numDel += Math.abs(secondArr.length - firstArr.length)
        
      return numDel
    }