javascript - Swap LexOrder Union Find -


so i'm doing interview practice questions , came across one: given string str , array of pairs indicates indices in string can swapped, return lexicographically largest string results doing allowed swaps. can swap indices number of times.

example

for str = "abdc" , pairs = [[1, 4], [3, 4]], output should swaplexorder(str, pairs) = "dbca".

by swapping given indices, strings: "cbda", "cbad", "dbac", "dbca". lexicographically largest string in list "dbca".

i've got working answer involving finding unions answer slow:

[time limit] 4000ms (js) 0 ≤ pairs.length ≤ 5000, pairs[i].length = 2. 1 ≤ str.length ≤ 10^4 

could me tweak code increase speed? heres code have:

  function swaplexorder(str, pairs) {     if (!pairs.length){         return str     }      answerhash = {}     unmoved = findunmoved(pairs, str.length)     unionsarr = findunions(pairs)       (i in unmoved){         answerhash[unmoved[i]] = str[(unmoved[i]-1)]     }      unionsarr.foreach(function(union){         letters = []         (i in union){             letters.push(str[(union[i]-1)])         }          letters.sort()         union.sort(function(a,b){             return b-a         })          (j in union){             answerhash[union[j]] = letters[j]         }      })      string = []     (keys in answerhash){         string.push(answerhash[keys])     }     return string.join('') }    //if 2 pairs share number belong in same array findunions = function(pairs, unions){    if (!unions){        unions = [pairs[0]];        pairs.shift();    }else{        if(pairs.length){            unions.push(pairs[0])            pairs.shift()        }    }      if (!pairs.length){         return unions     }      unite = true     while (unite && pairs.length){         unite = false         loop1:         (i in unions){             loop2:             (j in pairs){                 if (unions[i].includes(pairs[j][0])){                     unions[i].push(pairs[j][1])                     pairs.splice(j, 1)                     unite = true                     break loop1                 }else if (unions[i].includes(pairs[j][1])){                     unions[i].push(pairs[j][0])                     pairs.splice(j, 1)                     unite = true                     break loop1                 }             }         }     }     return findunions(pairs, unions) }   findunmoved = function(pairs, length){     range = []     (var i=1;i<length+1;i++){         range.push(i);     }     allnum = [].concat.apply([], pairs)     range = range.filter(function(x){         return (!allnum.includes(x))     })     return range } 

its function i'm using find unions thinking maybe without creating hash? if ya'll know of better method of solving problem i'm learning somethinig new. thanks!


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -