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
21 changes: 21 additions & 0 deletions HamSungJun/HashTable/Solution_409.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function longestPalindrome (s: string): number {
const charCountMap = new Map()
for (let i = 0; i < s.length; i++) {
const nextChar = s[i]
if (charCountMap.has(nextChar)) {
charCountMap.set(nextChar, charCountMap.get(nextChar) + 1)
} else {
charCountMap.set(nextChar, 1)
}
}
let sum = 0
for (const v of charCountMap.values()) {
if (v % 2 === 0) {
sum += v
} else if (v !== 1) {
sum += (v - 1)
}
}
/* 카운트가 1인 알파벳이 있었다면 그중에 하나만 채택해서 마지막 값에 더한다. */
return sum === s.length ? sum : sum + 1
};
16 changes: 16 additions & 0 deletions HamSungJun/HashTable/Solution_599.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function findRestaurant (list1: string[], list2: string[]): string[] {
const m1 = new Map()
const commonList: any[] = []
let minIndexSum = Number.MAX_SAFE_INTEGER
list1.forEach((v, i) => { m1.set(v, i) })
list2.forEach((v, i) => {
if (m1.has(v)) {
const nextIndexSum = m1.get(v) + i
minIndexSum = Math.min(minIndexSum, nextIndexSum)
if (minIndexSum === nextIndexSum) {
commonList.push([v, nextIndexSum])
}
}
})
return commonList.filter(([v, indexSum]) => indexSum === minIndexSum).map(([v, _]) => v)
};
32 changes: 32 additions & 0 deletions HamSungJun/HashTable/Solution_953.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function isAlienSorted (words: string[], order: string): boolean {
const sortMap = new Map()
for (let i = 0; i < order.length; i++) {
sortMap.set(order[i], i)
}
for (let i = 0; i < words.length - 1; i++) {
const w1 = words[i]
const w2 = words[i + 1]
if (isUnsorted(w1, w2, sortMap)) return false
}
return true
};

function isUnsorted (w1: string, w2: string, sortMap: Map<string, number>): boolean {
const minLength = Math.min(w1.length, w2.length)
for (let i = 0; i < minLength; i++) {
const weightW1 = sortMap.get(w1[i]) as number
const weightW2 = sortMap.get(w2[i]) as number
if (weightW1 > weightW2) {
return true
} else if (weightW1 === weightW2) {
if (i === minLength - 1 && w1.length > w2.length) {
return true
}
} else if (weightW1 < weightW2) {
return false
}
}
return false
}

console.log(isAlienSorted(['hello', 'leetcode'], 'hlabcdefgijkmnopqrstuvwxyz'))