diff --git a/HamSungJun/HashTable/Solution_350.ts b/HamSungJun/HashTable/Solution_350.ts new file mode 100644 index 0000000..2b9e87b --- /dev/null +++ b/HamSungJun/HashTable/Solution_350.ts @@ -0,0 +1,25 @@ +/** + * @param {number[]} nums1 + * @param {number[]} nums2 + * @return {number[]} + */ +const intersect = function (nums1: number[], nums2: number[]): number[] { + const countMap = new Map() + const result: number[] = [] + nums1.forEach(n1 => { + if (countMap.has(n1)) { + countMap.set(n1, countMap.get(n1) + 1) + } else { + countMap.set(n1, 1) + } + }) + nums2.forEach(n2 => { + if (countMap.has(n2) && countMap.get(n2) > 0) { + result.push(n2) + countMap.set(n2, countMap.get(n2) - 1) + } + }) + return result +} + +console.log(intersect([4, 9, 5], [9, 4, 9, 8, 4])) diff --git a/HamSungJun/HashTable/Solution_389.ts b/HamSungJun/HashTable/Solution_389.ts new file mode 100644 index 0000000..c3d8c4b --- /dev/null +++ b/HamSungJun/HashTable/Solution_389.ts @@ -0,0 +1,22 @@ +function findTheDifference (s: string, t: string): string { + const countMap = new Map() + let answer = '' + for (let i = 0; i < s.length; i++) { + const nextChar = s[i] + if (countMap.has(nextChar)) { + countMap.set(nextChar, countMap.get(nextChar) + 1) + } else { + countMap.set(nextChar, 1) + } + } + for (let i = 0; i < t.length; i++) { + const nextChar = t[i] + if (countMap.has(nextChar) && countMap.get(nextChar) > 0) { + countMap.set(nextChar, countMap.get(nextChar) - 1) + } else { + answer = nextChar + break + } + } + return answer +}; diff --git a/HamSungJun/HashTable/Solution_748.ts b/HamSungJun/HashTable/Solution_748.ts new file mode 100644 index 0000000..c5c4e23 --- /dev/null +++ b/HamSungJun/HashTable/Solution_748.ts @@ -0,0 +1,46 @@ +function shortestCompletingWord (licensePlate: string, words: string[]): string { + const licenseCountMap: Map = new Map() + words.sort((a, b) => { + const aLen = a.length + const bLen = b.length + return Number(aLen < bLen) - Number(aLen > bLen) + }) + console.log(words) + for (let i = 0; i < licensePlate.length; i++) { + const nextChar = licensePlate[i] + const nextCharCode = nextChar.charCodeAt(0) + if ((nextCharCode >= 65 && nextCharCode <= 90) || (nextCharCode >= 97 && nextCharCode <= 122)) { + const nextLowerChar = nextChar.toLowerCase() + if (licenseCountMap.has(nextLowerChar)) { + licenseCountMap.set(nextLowerChar, licenseCountMap.get(nextLowerChar) as number + 1) + } else { + licenseCountMap.set(nextLowerChar, 1) + } + } + } + for (let i = 0; i < words.length; i++) { + if (isCompletingWord(licenseCountMap, words[i])) { + return words[i] + } + } +}; + +function isCompletingWord (map: Map, word: string): boolean { + const wordCountMap = new Map() + for (let i = 0; i < word.length; i++) { + const nextChar = word[i] + if (wordCountMap.has(nextChar)) { + wordCountMap.set(nextChar, wordCountMap.get(nextChar) + 1) + } else { + wordCountMap.set(nextChar, 1) + } + } + for (const [k, v] of map) { + if (!wordCountMap.has(k)) { + return false + } else if (v > wordCountMap.get(k)) { + return false + } + } + return true +}