diff --git a/HamSungJun/HashTable/Solution_409.ts b/HamSungJun/HashTable/Solution_409.ts new file mode 100644 index 0000000..f31819b --- /dev/null +++ b/HamSungJun/HashTable/Solution_409.ts @@ -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 +}; diff --git a/HamSungJun/HashTable/Solution_599.ts b/HamSungJun/HashTable/Solution_599.ts new file mode 100644 index 0000000..ad06522 --- /dev/null +++ b/HamSungJun/HashTable/Solution_599.ts @@ -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) +}; diff --git a/HamSungJun/HashTable/Solution_953.ts b/HamSungJun/HashTable/Solution_953.ts new file mode 100644 index 0000000..8c64cfd --- /dev/null +++ b/HamSungJun/HashTable/Solution_953.ts @@ -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): 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'))