-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagram.js
60 lines (44 loc) · 1.39 KB
/
Anagram.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
function checkIfAnagram(s1, s2) {
let a = s1.split("").sort().join();
let b = s2.split("").sort().join();
a === b ? console.log("true") : console.log("false");
}
console.log(checkIfAnagram("test", "tes"));
function GroupAnagram(words) {
const anagrams = {};
for (let i = 0; i < words.length; i++) {
const sortedWord = words[i].split('').sort().join('');
if (anagrams[sortedWord]) {
anagrams[sortedWord].push(words[i])
}
else{
anagrams[sortedWord] = [words[i]]
}
}
return Object.values(anagrams);
}
console.log(GroupAnagram(["eat", "tea", "tan", "ate", "nat", "bat"]));
//An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
// typically using all the original letters exactly once.
function GroupAnagrams(words) {
const anagrams = {};
for (const word of words) {
const sortedWord = word.split("").sort().join("");
anagrams[sortedWord]
? anagrams[sortedWord].push(word)
: (anagrams[sortedWord] = [word]);
}
return Object.values(anagrams)
}
console.log(GroupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]));
var isAnagram = function(s, t) {
const arrayedT =t.split("").sort().join("");
const arrayedS = s.split("").sort().join('')
if(arrayedT===arrayedS){
return true
}
else{
return false
}
};
console.log(isAnagram("rat","cat"))