-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashTable.js
422 lines (406 loc) · 10.9 KB
/
hashTable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// 1. Two Sum
function TwoSum0001() {
// O(n^2)
var twoSum1 = function (nums, target) {
for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
result = [i, j]
return result
}
}
}
}
//O(n)
var twoSum = function (nums, target) {
let hashTable = {}
for (let i = 0; i < nums.length; i++) {
if (hashTable[target - nums[i]] !== undefined) {
//如果hashTable裡有對應的數字
console.log([hashTable[target - nums[i]], i])
return [hashTable[target - nums[i]], i] // return 相應位置
} else {
hashTable[nums[i]] = i //1. 將arr中的數字存為key並記錄位置
}
console.log([hashTable])
}
}
twoSum([1, 5, 11, 15, 16, 18, 20, 2, 7], 9)
}
// TwoSum0001()
//==================================================//
//290. Word Pattern
function WordPattern0290() {
//O(n) 66,86
var wordPattern = function (pattern, s) {
let hashTable = {}
let setTable = new Set([1,1,1,])//Set{1}
s = s.split(' ')
if (pattern.length !== s.length) return false
for (let i = 0; i < pattern.length; i++) {
if (!hashTable[pattern[i]] && !setTable.has(s[i])) {
hashTable[pattern[i]] = s[i]
setTable.add(s[i])
} else if (hashTable[pattern[i]] !== s[i]) {
return false
}
console.log(hashTable, setTable)
}
return true
}
/* // 'constructor' can not used as key so this method can not use
var wordPattern = function (pattern, s) {
let hashTable = {}
let setTable = {}
s = s.split(' ')
if (pattern.length !== s.length) return false
for (let i = 0; i < pattern.length; i++) {
if (!hashTable[pattern[i]] && setTable[s[i]] === undefined) {
hashTable[pattern[i]] = s[i]
setTable[s[i]] = i
} else if (hashTable[pattern[i]] !== s[i]) {
return false
}
console.log(hashTable, setTable)
}
return true
}*/
//O(n) 76,17
var wordPattern1 = function (pattern, s) {
str = s.split(' ')
if (str.length != pattern.length) {
return false
}
var hash = {}
for (var i = 0; i < str.length; i++) {
if (hash[pattern[i]]) {
if (hash[pattern[i]] !== str[i]) {
return false
}
} else {
//hashtable中是不是存在str中的值有則代表重複的對應字所以錯誤
if (Object.values(hash).indexOf(str[i]) !== -1) {
return false
} else {
hash[pattern[i]] = str[i]
}
}
}
return true
}
console.log(wordPattern('abba', 'dog dog dog dog'))
console.log(wordPattern('abba', 'dog constructor constructor dog'))
console.log(wordPattern('abba', 'dog cat cat dog'))
console.log(wordPattern('abc', 'c b a'))
console.log(wordPattern('abc', 'c b c'))
}
// WordPattern0290()
//==================================================//
// 383. Ransom Note
function RansomNote0383() {
//O(n) 43,45
var canConstruct = function (ransomNote, magazine) {
if (ransomNote.length > magazine.length) return false
let hashTable = {}
for (let i = 0; i < magazine.length; i++) {
if (ransomNote[i] === magazine[i]) {
continue
} else {
//加減兩字串中的字符
if (i < ransomNote.length) {
if (hashTable[ransomNote[i]] !== undefined) {
hashTable[ransomNote[i]] += 1
} else {
hashTable[ransomNote[i]] = 1
}
}
if (hashTable[magazine[i]] !== undefined) {
hashTable[magazine[i]] -= 1
} else {
hashTable[magazine[i]] = -1
}
}
console.log(hashTable)
}
//如果hashTable有值大於零代表錯誤
for (let key in hashTable) {
if (hashTable[key] > 0) return false
}
return true
}
console.log(canConstruct('aab', 'baa'))
console.log(canConstruct('aab', 'bba'))
//O(n)60,48
var canConstruct2 = function (ransomNote, magazine) {
if (ransomNote.length > magazine.length) return false
let hashTable = {}
//先將magazine加入hashTable
for (let i = 0; i < magazine.length; i++) {
if (hashTable[magazine[i]] !== undefined) {
hashTable[magazine[i]] += 1
} else {
hashTable[magazine[i]] = 1
}
}
for (let i = 0; i < ransomNote.length; i++) {
//hashTable中沒有ransomNote[i]
if (
hashTable[ransomNote[i]] === 0 ||
hashTable[ransomNote[i]] === undefined
) {
return false
} else {
hashTable[ransomNote[i]] -= 1
}
}
return true
}
//O(n) 39,84 #不知道為什麼這樣省記憶體
var canConstruct1 = function (ransomNote, magazine) {
const map = new Map()
for (let i = 0; i < magazine.length; i++) {
if (map.has(magazine[i])) {
map.set(magazine[i], map.get(magazine[i]) + 1)
} else {
map.set(magazine[i], 1)
}
}
for (let i = 0; i < ransomNote.length; i++) {
//Map中沒有ransomNote[i]
if (!map.has(ransomNote[i]) || map.get(ransomNote[i]) === 0) {
return false
}
map.set(ransomNote[i], map.get(ransomNote[i]) - 1)
}
return true
}
}
// RansomNote0383()
//==================================================//
//268. Missing Number
function MissingNumber0268() {
//O(n) 91,35
var missingNumber = function (nums) {
let missingNum = 0
nums.push(0)
for (let i = 0; i < nums.length; i++) {
missingNum += i - nums[i]
console.log(i, nums[i], missingNum)
}
return missingNum
}
//O(n) 99,75 與使用push比差很多
var missingNumber2 = function (nums) {
let missingNum = 0
nums[nums.length] = 0
for (let i = 0; i < nums.length; i++) {
missingNum += i - nums[i]
console.log(i, nums[i], missingNum)
}
return missingNum
}
console.log(missingNumber([3, 0, 1]))
}
// MissingNumber()
//==================================================//
//409. Longest Palindrome
function LongestPalindrome0409() {
//O(n) 86, 56
var longestPalindrome = function (s) {
let hashTable = {}
let oddExist = false
let len = 0
for (let i = 0; i < s.length; i++) {
if (hashTable[s[i]]) {
hashTable[s[i]] += 1
} else {
hashTable[s[i]] = 1
}
}
console.log(hashTable)
for (let key in hashTable) {
if (!oddExist && hashTable[key] % 2) {
oddExist = true
}
len += Math.floor(hashTable[key] / 2) * 2
}
return oddExist ? len + 1 : len
}
//O(n) 97, 22
var longestPalindrome2 = function (s) {
let oddset = new Set()
let len = 0
for (let i = 0; i < s.length; i++) {
if (oddset.has(s[i])) {
len += 2
oddset.delete(s[i])
} else {
oddset.add(s[i])
}
}
return oddset.size ? len + 1 : len
}
console.log(
longestPalindrome2(
'civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth'
)
)
}
// LongestPalindrome()
//==================================================//
//349. Intersection of Two Arrays
function IntersectionTwoArrays0349() {
//O(n) 95,16
var intersection = function (nums1, nums2) {
let hashTable = {}
let intersection = new Set()
for (let i = 0; i < nums1.length; i++) {
if (!hashTable[nums1[i]]) {
hashTable[nums1[i]] = 1
}
}
for (let i = 0; i < nums2.length; i++) {
if (hashTable[nums2[i]]) {
intersection.add(nums2[i])
}
}
return [...intersection]
}
//O(n^2) 80,96
var intersection2 = function (nums1, nums2) {
let intersection = []
//array.map O(n) array.includes O(n)
nums1.map(e => {
if (nums2.includes(e) && !intersection.includes(e)) {
intersection.push(e)
}
})
return intersection
}
console.log(intersection2([4, 9, 5], [9, 4, 9, 8, 4]))
}
// IntersectionTwoArrays()
//==================================================//
//1512. Number of Good Pairs
function NumberGoodPairs1512() {
//O(n) 23,30
var numIdenticalPairs = function (nums) {
//1個數->0
//2個數->1
//3個數->3
//4個數->6
//5個數->6+4
//10個數->9+8+7+6+5+4+3+2+1
//n個數->1到n-1連加= n*(n-1)/2
function countPairs2(number) {
let count = 0
for (let i = 0; i < number; i++){
for (let j = i + 1; j < number; j++) {
count++
}
}
return count
}
function countPairs(number) {
if (number <= 1) return 0
return (number * (number - 1)) / 2
}
let pairs = 0
let hashTable = {}
for (let i = 0; i < nums.length; i++) {
if (hashTable[nums[i]]) {
hashTable[nums[i]] += 1
} else {
hashTable[nums[i]] = 1
}
}
for (let key in hashTable) {
pairs += countPairs(hashTable[key])
}
return pairs
}
//O(n) 95,31
var numIdenticalPairs2 = function (nums) {
//1個數->0
//2個數->1
//3個數->3
//4個數->6
//5個數->6+4
//10個數->9+8+7+6+5+4+3+2+1
//n個數->1到n-1連加= n*(n-1)/2
let pairs = 0
let hashTable = {}
for (let i = 0; i < nums.length; i++) {
if (hashTable[nums[i]]) {
//n>1 ->配對數->前一項 + (n-1)
pairs += hashTable[nums[i]]
hashTable[nums[i]] += 1
} else {
hashTable[nums[i]] = 1
}
}
return pairs
}
}
//3. Longest Substring Without Repeating Characters
function LongestSubstring0003() {}
//17. Letter Combinations of a Phone Number
function LetterCombinations0017() {
const table = {
2: ['a', 'b', 'c'],
3: ['d', 'e', 'f'],
4: ['g', 'h', 'i'],
5: ['j', 'k', 'l'],
6: ['m', 'n', 'o'],
7: ['p', 'q', 'r', 's'],
8: ['t', 'u', 'v'],
9: ['w', 'x', 'y', 'z'],
}
var letterCombinations = function (digits) {
let arr = []
function recursion(arr,n) {
if (n<=1) {
return table[digits][0]
}
for(let i = 0;i<1000;i++){}
recursion()
}
}
letterCombinations('23')
}
// LetterCombinations()
// 217. Contains Duplicate
function ContainsDuplicate0217() {
//O(n) 91,61
var containsDuplicate = function (nums) {
let map = {}
for (let i = 0; i < nums.length; i++){
map[nums[i]] = (map[nums[i]] ?? 0) + 1
if (map[nums[i]]>1) return true
}
return false
}
console.log(containsDuplicate([1, 2, 3, 1]))
console.log(containsDuplicate([1, 2, 3, 4]))
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]))
}
// ContainsDuplicate0217()
//242. Valid Anagram
function ValidAnagram0242() {
//O(n) 87, 85
var isAnagram = function (s, t) {
if (s.length !== t.length) return false
let map = {}
for (let i = 0; i < s.length; i++){
map[s[i]] = (map[s[i]] ?? 0) + 1
map[t[i]] = (map[t[i]] ?? 0) - 1
console.log(map)
}
for (let key in map) {
if (map[key] !== 0) return false
}
return true
}
isAnagram('anagram', 'nagaram')
}
ValidAnagram0242()