Untitled
unknown
plain_text
10 months ago
1.8 kB
1
Indexable
// @leet start // // /** * @param {string} s * @return {number} */ var minimumSubstringsInPartition = function (s) { if (s.length === 0) return 0; let min = Number.MAX_VALUE; let map = new Map(); const checkValid = (currentMap, char) => { // currentMap.set(char, currentMap.get(char) + 1 || 1); // console.log({ currentMap }); const no = currentMap.get(char); let num = 0; currentMap.forEach((v) => { if (v === no) num++; }); // console.log({ curMap, "curMap.size": curMap.size, no, num }); return num === currentMap.size; }; const cal = (suffix, curMap, total = 0, index = 0) => { // if (total >= min && min !== Number.MAX_VALUE) return; if (index >= s.length) return; let newTotal = total; const newIdx = index; const c = s[newIdx]; suffix += c; // map.set(c, map.get(c) + 1 || 1) console.log({ char: c, newIdx }); curMap.set(c, curMap.get(c) + 1 || 1); if (suffix === "ddg") console.log("really", { curMap, suffix, char: c }); // check if current substring is valid const isValid = checkValid(curMap, c); console.log({ isValid }); // THEN reset suffix, continue to count if (isValid) { // newTotal += 1; // total++; console.log("VALID", { suffix, total, newIdx }); if (newIdx === s.length - 1) { console.log("index === s.length - 1", { newIdx, newTotal }); min = Math.min(newTotal, min); return; } // cal(suffix, total, count, index + 1); console.log("OUTSIDE", { total }); cal("", new Map(), newTotal + 1, newIdx + 1); } cal(suffix, new Map(map), newTotal, newIdx + 1); // OR continue add next char into suffix }; cal("", map); return min; }; // @leet end
Editor is loading...
Leave a Comment