Created
November 20, 2020 17:04
-
-
Save abha57/fee5745e46a6fe3a66379ea0b15e15da to your computer and use it in GitHub Desktop.
chrome sources code
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
| permutations | |
| const possiblePermutations = (str) => { | |
| const result = []; | |
| let dupStr = ''; | |
| let tempStr = str; | |
| result.push(str); | |
| for(let i = 0; i < str.length; i++) { | |
| // if(i === 0) { | |
| // result.push(str); | |
| // continue; | |
| // } | |
| for(let j = i + 1; j < str.length; j++) { | |
| // if(i === j) continue; | |
| dupStr = [...str]; | |
| dupStr[i] = str[j]; | |
| dupStr[j] = str[i]; | |
| result.push(dupStr.join('')); | |
| } | |
| let k = i + 1; | |
| str = str.split(''); | |
| const char = str.splice(k, 1); | |
| str.unshift(char[0]); | |
| tempStr = str.join(''); | |
| debugger; | |
| } | |
| return result; | |
| } | |
| console.log('possiblePermutations', possiblePermutations('abc')); | |
| =================================================== | |
| merging intervals | |
| const mergeTwoArrays = (arr1, arr2) => { | |
| const merged = []; | |
| if(arr1.length === 0) { | |
| return { | |
| array: arr2, | |
| ifMerged: false | |
| }; | |
| } | |
| if(arr2.length === 0) { | |
| return { | |
| array: arr1, | |
| ifMerged: false | |
| }; | |
| } | |
| if(arr1[1] < arr2[0] || arr2[1] < arr1[0]) { | |
| return { | |
| array: arr1, | |
| ifMerged: false | |
| }; | |
| } | |
| const smallest = arr1[0] < arr2[0] ? arr1[0] : arr2[0]; | |
| const largest = arr1[1] > arr2[1] ? arr1[1] : arr2[1]; | |
| merged[0] = smallest; | |
| merged[1] = largest; | |
| return { | |
| array: merged, | |
| ifMerged: true | |
| }; | |
| } | |
| const mergeAgg = (arr) => { | |
| if(!arr.length || arr.length === 1) return arr; | |
| let merged = null; | |
| let isFullyMerged = false; | |
| let count = 0; | |
| while(!isFullyMerged) { | |
| merged = mergeTwoArrays(arr[0], arr[1]); | |
| if(merged.ifMerged) { | |
| arr = [merged.array, arr.splice(2)]; | |
| } else { | |
| isFullyMerged = true; | |
| } | |
| } | |
| return arr; | |
| } | |
| const mergingIntervals = (arr) => arr.reduce((agg, ele) => { | |
| if(agg.length === 0) { | |
| agg = [ele]; | |
| return agg; | |
| } | |
| let singleEle = null; | |
| let mergingIndex = -1; | |
| for(let i = 0; i < agg.length; i++) { | |
| singleEle = mergeTwoArrays(agg[i], ele); | |
| if(singleEle.ifMerged) { | |
| mergingIndex = i; | |
| agg[i] = singleEle.array; | |
| break; | |
| } | |
| } | |
| if(mergingIndex > -1) { | |
| agg = [...agg.slice(0, mergingIndex), ...mergeAgg(agg.slice(mergingIndex))]; | |
| } else { | |
| agg = [...agg, ele]; | |
| } | |
| return agg; | |
| }, []); | |
| console.log('mergingIntervals', mergingIntervals([[1,3], [4,8], [7,12], [14,16], [15,18], [20,23], [24,26], [22, 30]])); | |
| ========================================= | |
| longest substring with k unique characters | |
| const kUniqueChar = (str, K) => { | |
| let resultStrs = []; | |
| let startIndex = 0; | |
| let endIndex = 0; | |
| let intermediateIndex = 0; | |
| let currentUniqueChars = 0; | |
| let objMap = {}; | |
| debugger; | |
| while (endIndex < str.length) { | |
| if (!objMap[str[endIndex]] || endIndex === str.length - 1) { | |
| if (currentUniqueChars === K) { | |
| resultStrs.push(str.substring(startIndex, endIndex)); | |
| startIndex = intermediateIndex; | |
| endIndex = intermediateIndex; | |
| objMap = {}; | |
| currentUniqueChars = 0; | |
| } else { | |
| objMap[str[endIndex]] = true; | |
| if(Object.keys(objMap).length === 2) { | |
| intermediateIndex = endIndex; | |
| } | |
| currentUniqueChars++; | |
| if(currentUniqueChars === K && endIndex === str.length - 1) { | |
| resultStrs.push(str.substring(startIndex)); | |
| } | |
| } | |
| } | |
| endIndex++; | |
| } | |
| if(resultStrs.length === 0) { | |
| return 'error'; | |
| } | |
| let maxLength = 0; | |
| let maxLengthStr = ""; | |
| resultStrs.forEach((s) => { | |
| if (s.length > maxLength) { | |
| maxLength = s.length; | |
| maxLengthStr = s; | |
| } | |
| }); | |
| return maxLengthStr; | |
| }; | |
| console.log("kUniqueChar", kUniqueChar("aaabbbbbbb", 3)); | |
| =================================================================== | |
| // array elem which has left and right sum equal. | |
| // [1,2,3,5,6] => 5 | |
| // [3,4,5,6,2,6,6,6] => 2 | |
| const leftRightSumBalance = (arr) => { | |
| const sum = arr.reduce((agg, ele) => agg + ele); | |
| let leftSum = 0, rightSum = sum; | |
| for(let i = 0; i < arr.length; i++) { | |
| leftSum = arr[i - 1] ? leftSum + arr[i - 1] : 0; | |
| rightSum -= arr[i]; | |
| if(leftSum === rightSum) return {index: i, ele: arr[i]}; | |
| } | |
| } | |
| console.log('leftRightSumBalance', leftRightSumBalance([10,2,2,4,4])); | |
| ================================================================ | |
| largest number | |
| const generateMax = (arr) => { | |
| let max = '0'; | |
| for(let i = 0; i < arr.length; i++) { | |
| let j = 0; | |
| while(j < max.length) { | |
| if(arr[i] >= max[j] ) { | |
| max = max.substr(0, j) + arr[i] + max.substr(j); | |
| break; | |
| } | |
| if(j === max.length - 1) { | |
| max += arr[i]; | |
| } | |
| j++; | |
| } | |
| } | |
| return max; | |
| } | |
| const largestNum = (arr1, arr2) => { | |
| if (arr1.length > arr2.length) return arr1; | |
| if(arr2.length > arr1.length) return arr2; | |
| firstMax = generateMax(arr1); | |
| secondMax = generateMax(arr2); | |
| return firstMax >= secondMax ? firstMax : secondMax; | |
| } | |
| console.log('max', largestNum([6,8,0,0,5,6,7,6,7,4,3,6,9,9], | |
| [5,9,6,7,4,3,5,6,8,3,5,6,7,6])); | |
| ========================================================= | |
| // [1,2,3,4,5], [2,3,4] // sorted arrays | |
| // [2,3,4] | |
| const duplicates = (arr1, arr2) => { | |
| let i = 0, j = 0; | |
| const dupArr = []; | |
| const shorter = arr1.length > arr2.length ? arr2 : arr1; | |
| const longer = shorter.length === arr1.length ? arr2 : arr1; | |
| while(j < shorter.length) { | |
| if(longer[i] === shorter[j]) { | |
| dupArr.push(longer[i]); | |
| i++; | |
| j++; | |
| } else if(longer[i] < shorter[j]) { | |
| i++; | |
| } else { | |
| j++; | |
| } | |
| } | |
| return dupArr; | |
| } | |
| console.log('duplicates', duplicates([1,3,9,10,17,18], [1,4,9,10])); | |
| ================================================================= | |
| clicks by domain | |
| // const counts = [ | |
| // "900,google.com", | |
| // "60,mail.yahoo.com", | |
| // "10,mobile.sports.yahoo.com", | |
| // "40,sports.yahoo.com", | |
| // "300,yahoo.com", | |
| // "10,stackoverflow.com", | |
| // "20,overflow.com", | |
| // "2,en.wikipedia.org", | |
| // "1,m.wikipedia.org", | |
| // "1,mobile.sports", | |
| // "1,google.co.uk" | |
| // ]; | |
| const knownEnds = [ | |
| '.com', | |
| '.org', | |
| '.co.uk', | |
| '.sports' | |
| ]; | |
| const trimKnownDomains = (str) => { | |
| for(let i = 0; i < knownEnds.length; i++) { | |
| if(str.endsWith(knownEnds[i])) { | |
| const index = str.indexOf(knownEnds[i]); | |
| str = str.substring(0, index); | |
| } | |
| } | |
| return str.split('.'); | |
| } | |
| const clicksByDomain = (arrayInput) => { | |
| const domainObj = {}; | |
| const arrayLen = arrayInput.length; | |
| for(let i = 0; i < arrayLen; i++) { | |
| const split = arrayInput[i].split(','); | |
| const count = Number(split[0]); | |
| const domain = split[1]; | |
| const domains = trimKnownDomains(domain); | |
| console.log('domains', domains); | |
| let subdomain = ''; | |
| for(let j = domains.length - 1; j >= 0; j--) { | |
| if( j !== domains.length - 1) { | |
| subdomain += '.' + domains[j]; | |
| } else { | |
| subdomain = domains[j]; | |
| } | |
| if(domainObj[subdomain]) { | |
| domainObj[subdomain] += count; | |
| } else { | |
| domainObj[subdomain] = count; | |
| } | |
| } | |
| console.log('domainObj', domainObj); | |
| } | |
| } | |
| clicksByDomain([ | |
| "900,google.com", | |
| "60,mail.yahoo.com", | |
| "10,mobile.sports.yahoo.com", | |
| "40,sports.yahoo.com", | |
| "300,yahoo.com", | |
| "10,stackoverflow.com", | |
| "20,overflow.com", | |
| "2,en.wikipedia.org", | |
| "1,m.wikipedia.org", | |
| "1,mobile.sports", | |
| "1,google.co.uk" | |
| ]); | |
| ============================================================ | |
| alphabet fibonacci | |
| // 'AABBCC' => 4 | |
| // 'EEDDCC' => 12 | |
| // 'AB' => 0 + 1 = 1 | |
| // A = 0, B = 1, C = A + B, D = C + B, E = D + C | |
| const alphabetArr = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']; | |
| const cache = { | |
| A: 0, | |
| B: 1 | |
| }; | |
| const computeAlpNum = (alphabet) => { | |
| if(alphabet === 'A') return cache.A; | |
| if(alphabet === 'B') return cache.B; | |
| if(cache[alphabet]) return cache[alphabet]; | |
| const index = alphabetArr.indexOf(alphabet); | |
| cache[alphabet] = computeAlpNum(alphabetArr[index - 1]) + computeAlpNum(alphabetArr[index - 2]); | |
| return cache[alphabet]; | |
| } | |
| const alphabetSum = (str) => { | |
| const length = str.length; | |
| let sum = 0; | |
| for(let i = 0; i < str.length; i++) { | |
| sum += computeAlpNum(str[i]); | |
| } | |
| return sum; | |
| } | |
| console.log('alphabetSum', alphabetSum('EEDDCC')); | |
| ========================================================== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment