[JavaScript] 2904. Shortest and Lexicographically Smallest Beautiful String
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n---\n\n```\nvar shortestBeautifulSubstring = function (s, k) {\n  s = s.replaceAll(/^[0]*/g, ""); // remove 0s at begin\n\n  let n = s.length;\n  let pre = new Array(n);\n  for (let i = 0, sum = 0; i < n; i++) {\n    pre[i] = sum += +s[i]; // prefix sum   of ones\n  }\n\n  let smallestSubStr = "";\n  for (let i = -1, j = 0; j < n; j++) {\n    let numOfOnes = pre[j] - (pre[i] || 0); // sum difference of ones\n    if (numOfOnes === k) {\n      for (; i < j; i++) {\n        let numOfOnes = pre[j] - (pre[i] || 0);\n        if (numOfOnes < k) break;\n        let subStr = s.slice(i + 1, j + 1);\n        if (\n          !smallestSubStr || // 1st substring, take it\n          subStr.length < smallestSubStr.length || // take new smaller substring\n          (subStr.length === smallestSubStr.length && subStr < smallestSubStr) // take new lexicographically smaller substring\n        ) {\n          smallestSubStr = subStr;\n        }\n      }\n    }\n  }\n  return smallestSubStr;\n};\n```\n\n---\n\n**Weekly Contest 367**\n- Q1 - https://leetcode.com/problems/find-indices-with-index-and-value-difference-i/solutions/4169852/javascript-2903-find-indices-with-index-and-value-difference-i/\n- Q2 - See above\n- Q3 - https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii/solutions/4169858/javascript-2905-find-indices-with-index-and-value-difference-ii/\n\n---\n
['JavaScript']