-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7kyuAnagramDetection.js
53 lines (39 loc) · 1.3 KB
/
7kyuAnagramDetection.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
//An anagram is the result of rearranging the letters of a word to produce a new word.
// Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise.
// write the function isAnagram
function isAnagram(test, original) {
test = test.toLowerCase();
original = original.toLowerCase();
if (test.length !== original.length) {
return false;
}
let str1 = test.split("").sort().join("");
let str2 = original.split("").sort().join("");
let result = (str1 === str2);
return result;
};
//solution 2
function isAnagram(test, original) {
return test.toLowerCase().split("").sort().join("") === original.toLowerCase().split("").sort().join("");
}
//solution 3
const isAnagram = (test, original) => {
const word1 = test.toLowerCase().split('').sort().join('');
const word2 = original.toLowerCase().split('').sort().join('');
return word1 === word2;
}
//optimal solution
function isAnagram(test, original) {
test = test.toLowerCase();
original = original.toLowerCase();
if (test.length !== original.length) return false;
const charCount = {};
for (let char of original) {
charCount[char] = (charCount[char] || 0) + 1;
}
for (let char of test) {
if (!charCount[char]) return false;
charCount[char]--;
}
return true;
}