Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions HamSungJun/HashTable/Solution_350.ts
Original file line number Diff line number Diff line change
@@ -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]))
22 changes: 22 additions & 0 deletions HamSungJun/HashTable/Solution_389.ts
Original file line number Diff line number Diff line change
@@ -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
};
46 changes: 46 additions & 0 deletions HamSungJun/HashTable/Solution_748.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function shortestCompletingWord (licensePlate: string, words: string[]): string {
const licenseCountMap: Map<string, number> = 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<string, number>, 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
}