❇shortest-and-lexicographically-smallest-beautiful-string👌 🏆O(N)❤️ Javascript🎯 Runtime 👀89.29%🕕
Time Complexity: O(N)\nSpace Complexity: O(N)\n\n\nfunction findMinimumString(string1, string2) {\n    if (string1 === string2) {\n        return string1;\n    
Time Complexity: O(N)\nSpace Complexity: O(N)\n\n```\nfunction findMinimumString(string1, string2) {\n    if (string1 === string2) {\n        return string1;\n    }\n    if (string1.length === string2.length) {\n        for (let index = 0; index < string1.length; index++) {\n            if (string1[index] === string2[index]) {\n                continue;\n            }\n            if (string1[index] === `0`) {\n                return string1;\n            }\n            if (string2[index] === `0`) {\n                return string2;\n            }\n        }\n        return string1;\n    } else {\n        if (string1.length > string2.length) {\n            return string2;\n        }\n        return string1;\n    }\n}\n\nvar shortestBeautifulSubstring = function (s, k) {\n    let nextOneIndex = null;\n    let countOnes = 0;\n    let minNumber = ``;\n    let pleaseStoreForSubstring = false;\n    let storingBinarySubstring = ``;\n    for (let index = 0; index < s.length; ) {\n        if (storingBinarySubstring !== `` && s[index] === \'1\' && nextOneIndex === null) {\n            nextOneIndex = index;\n        }\n        if (s[index] === \'1\' || pleaseStoreForSubstring) {\n            storingBinarySubstring += s[index];\n            pleaseStoreForSubstring = true;\n            if (s[index] === \'1\') {\n                countOnes++;\n            }\n        }\n        if (\n            countOnes === k ||\n            (minNumber !== `` && minNumber.length < storingBinarySubstring.length)\n        ) {\n            if (minNumber !== ``) {\n                minNumber = findMinimumString(minNumber, storingBinarySubstring);\n            } else {\n                minNumber = storingBinarySubstring;\n            }\n            storingBinarySubstring = ``;\n            countOnes = 0;\n            if (k !== 1) {\n                index = nextOneIndex || ++index;\n            } else {\n                index++;\n            }\n            pleaseStoreForSubstring = false;\n            nextOneIndex = null;\n        } else {\n            index++;\n        }\n    }\n    return minNumber;\n};\n```\n\n![image](https://assets.leetcode.com/users/images/ea3e78a8-2381-4c47-b9af-9c91077dae2b_1705581126.0873926.png)\n
['JavaScript']