Created
January 10, 2019 03:42
-
-
Save j-quelly/09fafd5ca2dfe83be9cc5aebadf5d321 to your computer and use it in GitHub Desktop.
Given a string str and array of pairs that indicates which indices in the string can be swapped, return the lexicographically largest string that results from doing the allowed swaps. You can swap indices any number of times.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quickSort(nums) { | |
// base case | |
if (nums.length <= 1) { | |
return nums; | |
} | |
// grab a pivot | |
const pivot = nums.pop(); | |
// divide | |
const smaller = []; | |
const larger = []; | |
while (nums.length) { | |
if (nums[0] > pivot) { | |
smaller.push(nums.shift()); | |
} else { | |
larger.push(nums.shift()); | |
} | |
} | |
return [].concat(quickSort(smaller), pivot, quickSort(larger)); | |
}; | |
function getConnections(str, pairs) { | |
let results = []; | |
let chunk = []; | |
while(pairs.length) { | |
const a = pairs[0][0] - 1; | |
const b = pairs[0][1] - 1; | |
if (!chunk.length) { | |
chunk.push(a, b); | |
pairs.shift(); | |
continue; | |
} | |
const connected = pairs.find((pair) => chunk.includes(pair[0] - 1) || chunk.includes(pair[1] - 1)); | |
const connectedIndex = pairs.findIndex((pair) => chunk.includes(pair[0] - 1) || chunk.includes(pair[1] - 1)); | |
if (Array.isArray(connected)) { | |
const foundA = connected[0] - 1; | |
const foundB = connected[1] - 1; | |
if (!chunk.includes(foundA)) { | |
chunk.push(foundA); | |
} | |
if (!chunk.includes(foundB)) { | |
chunk.push(foundB); | |
} | |
pairs.splice(connectedIndex, 1); | |
} else { | |
results.push(chunk); | |
chunk = []; | |
chunk.push(a, b); | |
} | |
} | |
results.push(chunk); | |
const chars = results.map((chunk) => { | |
return chunk.map((index) => { | |
return str[index]; | |
}); | |
}); | |
const returns = { chars, indices: results }; | |
return returns; | |
} | |
function swapLexOrder(str, pairs) { | |
const set = new Set(); | |
const charMap = {}; | |
pairs.forEach((pair) => { | |
const a = pair[0] - 1; | |
const b = pair[1] - 1; | |
set.add(a); | |
set.add(b); | |
if (!charMap[str[a]]) { | |
charMap[str[a]] = true; | |
} | |
if (!charMap[str[b]]) { | |
charMap[str[b]] = true; | |
} | |
}); | |
const swappableIndices = [...set.values()]; | |
const hashMap = str.split('').map((char, i) => { | |
if (swappableIndices.includes(i)) { | |
return undefined; | |
} | |
return char; | |
}); | |
// then sort the remaining characters that can be swapped | |
const swappableChars = getConnections(str, [...pairs]); | |
const sorted = swappableChars.chars.map((arr) => { | |
return quickSort(arr).join(''); | |
}).join('').split(''); | |
const sortedIndices = swappableChars.indices.map((arr) => { | |
const der = [...quickSort(arr)]; | |
der.reverse(); | |
return der; | |
}); | |
sortedIndices.forEach((chunk, i) => { | |
chunk.forEach((index) => { | |
hashMap[index] = sorted.splice(0,1)[0]; | |
}); | |
}); | |
return hashMap.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment