forked from spandey1296/CODEMONK-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.js
35 lines (23 loc) Β· 856 Bytes
/
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
// example
/**
* Sort - HeapSort Space O(1) | QuickSort Space O(log(N))
* Time O(N * logN) | Space O(N)
* https://leetcode.com/problems/valid-anagram/
* @param {string} s
* @param {string} t
* @return {boolean}
*/
// 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.
var isAnagram = (s, t) => {
const isEqual = s.length === t.length;
if (!isEqual) return false;
return reOrder(s) === reOrder(t);
}
const reOrder = str => str
.split('')
.sort((a, b) => a.localeCompare(b))
.join("");
console.log(isAnagram("mango", "goman")) //returns true
console.log(isAnagram("rat", "tas")) //returns false
console.log(isAnagram("parrot", "aorrpt")) //returns true
console.log(isAnagram("water", "terwe")) //returns false