|
| 1 | +/** |
| 2 | + * @param {string} s |
| 3 | + * @return {number} |
| 4 | + */ |
| 5 | +const lengthOfLongestSubstring = (s) => { |
| 6 | + // Put into a map {index: 0, char: "a", nbAppeared: 1} |
| 7 | + let tmpMap = new Map(), |
| 8 | + charList = s.split(''), |
| 9 | + tmpList = [], // Deciding result |
| 10 | + result = [], |
| 11 | + prevResult; |
| 12 | + |
| 13 | + console.log("charList") |
| 14 | + console.log(charList) |
| 15 | + console.log(charList.length) |
| 16 | + |
| 17 | + // if (1 >= charList.length) return charList.length; |
| 18 | + |
| 19 | + var prevTmpList = []; |
| 20 | + |
| 21 | + for (let i = 0; i < charList.length; i++) { |
| 22 | + // Might need extra handle if there is upper/lower case. |
| 23 | + let currChar = charList[i], |
| 24 | + existCharMap = tmpMap.get(currChar); |
| 25 | + |
| 26 | + console.log(`________________ i: ${i}, currChar: ${currChar},`); |
| 27 | + |
| 28 | + if (existCharMap) { |
| 29 | + // Reset the tmp result 2nd time onwards |
| 30 | + console.log("yes existCharMap") |
| 31 | + |
| 32 | + console.log("existCharMap") |
| 33 | + console.log(existCharMap) |
| 34 | + |
| 35 | + // if ((existCharMap.index + 1) === i) { |
| 36 | + // if (tmpList[0] === currChar) { |
| 37 | + // console.log("shifting tmpList") |
| 38 | + // tmpList.shift(); |
| 39 | + // tmpList.push(currChar); |
| 40 | + // console.log(tmpList) |
| 41 | + // } else { |
| 42 | + |
| 43 | + console.log("tmpList.indexOf(currChar)") |
| 44 | + console.log(tmpList.indexOf(currChar)) |
| 45 | + if (tmpList.includes(currChar)) { |
| 46 | + console.log("------------- existing char, new tmp!!!"); |
| 47 | + |
| 48 | + console.log(prevTmpList) |
| 49 | + console.log(tmpList) |
| 50 | + |
| 51 | + if (tmpList.length > prevTmpList.length) { |
| 52 | + console.log("------------- replacing bc >"); |
| 53 | + prevTmpList = [...tmpList]; |
| 54 | + } else { |
| 55 | + console.log("------------- idc"); |
| 56 | + |
| 57 | + } |
| 58 | + |
| 59 | + let nbRemove = tmpList.indexOf(currChar); |
| 60 | + for (let j = 0; j <= nbRemove; j++) { |
| 61 | + console.log("shift loop") |
| 62 | + tmpList.shift(); |
| 63 | + } |
| 64 | + tmpList.push(currChar); |
| 65 | + |
| 66 | + console.log("QHIUSJHHJDSAKHJDKJHASDKJHDKJSAD") |
| 67 | + console.log(prevTmpList) |
| 68 | + console.log(tmpList) |
| 69 | + |
| 70 | + // if (tmpList[tmpList.length - 1] == currChar) { |
| 71 | + // tmpList = [currChar] |
| 72 | + // } else { |
| 73 | + // tmpList = [tmpList[tmpList.length - 1], currChar]; // new list |
| 74 | + // } |
| 75 | + } else { |
| 76 | + for (let j = 0; j <= tmpList.indexOf(currChar); j++) { |
| 77 | + console.log("shift loop") |
| 78 | + tmpList.shift(); |
| 79 | + } |
| 80 | + tmpList.push(currChar); |
| 81 | + } |
| 82 | + |
| 83 | + // Update values |
| 84 | + existCharMap.nbAppeared += 1; |
| 85 | + tmpMap.set(currChar, existCharMap); |
| 86 | + // } |
| 87 | + } else { |
| 88 | + // Set it into the map if doesnt exists |
| 89 | + tmpMap.set(currChar, { index: i, nbAppeared: 1 }); |
| 90 | + tmpList.push(currChar) |
| 91 | + } |
| 92 | + |
| 93 | + console.log("____________________ output each loop ____________________") |
| 94 | + console.log("tmpList") |
| 95 | + console.log(tmpList) |
| 96 | + console.log("prevTmpList") |
| 97 | + console.log(prevTmpList) |
| 98 | + |
| 99 | + result = prevTmpList.length > tmpList.length ? prevTmpList : tmpList; |
| 100 | + } |
| 101 | + |
| 102 | + console.log("final output:") |
| 103 | + console.log(tmpMap) |
| 104 | + console.log(result) |
| 105 | + console.log(result.length) |
| 106 | + console.log("charList output:") |
| 107 | + console.log(charList.length) |
| 108 | + console.log(charList[0]) |
| 109 | + console.log(charList[1]) |
| 110 | + console.log(charList[0] !== charList[1]) |
| 111 | + |
| 112 | + console.log(result.length) |
| 113 | + |
| 114 | + return result.length; |
| 115 | +}; |
| 116 | + |
| 117 | +// lengthOfLongestSubstring("abcabcbb") // abc |
| 118 | +// lengthOfLongestSubstring("bbbbb") // b |
| 119 | +// lengthOfLongestSubstring("pwwkew") // wke |
| 120 | +// lengthOfLongestSubstring("dvdf") // vdf |
| 121 | +// lengthOfLongestSubstring("aa") // 1 |
| 122 | +// lengthOfLongestSubstring(" ") // 1 |
| 123 | +// lengthOfLongestSubstring("au") // 2 |
| 124 | +// lengthOfLongestSubstring("cdd") // 1 |
| 125 | +// lengthOfLongestSubstring("anviaj") // 5 |
| 126 | +lengthOfLongestSubstring("ckilbkd") // 5 |
0 commit comments