Created
February 19, 2022 00:42
-
-
Save germanescobar/f4ee6345f8d03fd652f4eff79175cbb2 to your computer and use it in GitHub Desktop.
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
| 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 | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No funciona cuando hay 0's intermedios.