-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmap.js
More file actions
89 lines (44 loc) · 1.39 KB
/
Copy pathmap.js
File metadata and controls
89 lines (44 loc) · 1.39 KB
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
function firstNonRepeatingChar(str){
const charCount = new Map();
for(const char of str){
charCount.set(char, (charCount.get(char) || 0 ) + 1);
}
for(const char of str){
if(charCount.get(char) === 1 ) return char;
}
return null;
}
console.log(firstNonRepeatingChar("aabbccdeffg"))
function twoSum(numbers,target){
const map = new Map();
for(let i = 0; i < numbers.length; i++){
const complement = target - numbers[i];
if(map.has(complement)){
return [map.get(complement), i]
}
map.set(numbers[i],i);
}
return null;
}
console.log(twoSum([2, 7, 11, 15], 9));
function groupAnagrams(words){
const anagramMap = new Map();
for(const word of words){
let sortedWord = word.split("").sort().join("");
if(!anagramMap.has(sortedWord)){
anagramMap.set(sortedWord,[]);
}
anagramMap.get(sortedWord).push(word);
}
return Array.from(anagramMap.values());
}
console.log(groupAnagrams(["bat", "tab", "cat", "act", "tac"]));
function wordFrequency(text) {
const wordMap = new Map();
const words = text.toLowerCase().match(/\b\w+\b/g);
for (const word of words) {
wordMap.set(word, (wordMap.get(word) || 0) + 1);
}
return wordMap;
}
console.log(wordFrequency("This is a test. This test is easy."));