-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_22_2025.js
46 lines (40 loc) · 1.25 KB
/
1_22_2025.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
/**
* @param {string} s
* @param {string} t
* @return {boolean}
* basic implementation of isAnagram
* Better solution is to use a hash table
* iterate through the first string and add each character to the hash table
* iterate through the second string and check if each character is in the hash table
* if it is, remove it from the hash table
* if it is not, return false
* if the hash table is empty, return true, etc.
*/
const isAnagram = (s, t) => {
if (s.length != t.length) return false;
let sortedOne = s.split('').sort();
let sortedTwo = t.split('').sort();
for (let idx = 0; idx < sortedOne.length; idx++) {
if (sortedOne[idx] != sortedTwo[idx]) return false
}
return true;
};
/**
* @param {string} columnTitle
* @return {number}
*/
var titleToNumber = function(columnTitle) {
// This process is similar to
// binary-to-decimal conversion
// 1. Start from the rightmost character,
// 2. Multiply the value of each character by 26^i
// 3. Add them all together
// 4. Return the result
let result = 0;
for (let i = 0; i < columnTitle.length; i++)
{
result *= 26;
result += columnTitle[i].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
}
return result;
};