Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created February 19, 2022 00:42
Show Gist options
  • Select an option

  • Save germanescobar/f4ee6345f8d03fd652f4eff79175cbb2 to your computer and use it in GitHub Desktop.

Select an option

Save germanescobar/f4ee6345f8d03fd652f4eff79175cbb2 to your computer and use it in GitHub Desktop.
var removeKdigits = function(num, k) {
if (k == num.length) {
return "0"
}
if (k == 0 || num.length == 1) {
return num
}
const substr = num.substring(0, num.length - k + 1)
const minPos = calculateMinPos(substr)
let nextPos = minPos
if (num[minPos] === "0") {
nextPos++
}
const comp = num.substring(nextPos + 1)
const result = num[num[minPos] === 0 ? nextPos : minPos]
return (result === "0" ? "" : result) + removeKdigits(comp, k - minPos)
};
function calculateMinPos(str) {
let min = str[0]
let pos = 0
for (let i=1; i < str.length; i++) {
if (str[i] < min) {
min = str[i]
pos = i
}
}
return pos
}
@germanescobar
Copy link
Author

No funciona cuando hay 0's intermedios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment