Skip to content

Commit f479af2

Browse files
authored
Create 2273-find-resultant-array-after-removing-anagrams.js
1 parent c5bf290 commit f479af2

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param {string[]} words
3+
* @return {string[]}
4+
*/
5+
const removeAnagrams = function(words) {
6+
const res = []
7+
const n = words.length
8+
9+
for(let i = 0; i < n;) {
10+
let j = i + 1
11+
while(j < n && isAna(words[i], words[j])) j++
12+
res.push(words[i])
13+
i = j
14+
}
15+
return res
16+
17+
function isAna(s1, s2) {
18+
const arr = Array(26).fill(0)
19+
const a = 'a'.charCodeAt(0)
20+
for(let i = 0; i < s1.length; i++) {
21+
arr[s1.charCodeAt(i) - a]++
22+
}
23+
for(let i = 0; i < s2.length; i++) {
24+
arr[s2.charCodeAt(i) - a]--
25+
}
26+
for(const e of arr) {
27+
if(e !== 0) return false
28+
}
29+
return true
30+
}
31+
};
32+

0 commit comments

Comments
 (0)