Skip to content

Commit fa9b779

Browse files
committed
新增3838另一解法
1 parent d9ddd75 commit fa9b779

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

javascript/LeetCode/Array/3838.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @return {string}
1010
*/
1111
var mapWordWeights = function(words, weights) {
12+
// 解法1,使用map
1213
// 可能遇到的狀況:key(字母)、value(數字)重複出現
1314
let alp = generateAlphabet();
1415
let sumWeights = [];
@@ -54,7 +55,30 @@ var mapWordWeights = function(words, weights) {
5455
}
5556
return alp;
5657
}
58+
5759
};
60+
61+
/**
62+
* 解法2。沒有另寫涵式產生26個英文字母
63+
* 此法較快
64+
*
65+
* @param {*} words
66+
* @param {*} weights
67+
* @returns
68+
*/
69+
var mapWordWeights2 = function(words, weights) {
70+
let sumWeights = [];
71+
72+
for (const element of words) {
73+
let countLen = 0;
74+
for(let i = 0;i < element.length;++i) {
75+
countLen += weights[element.charCodeAt(i) - 'a'.charCodeAt()]
76+
}
77+
// String.fromCharCode(ascii code) => ascii code to char.
78+
sumWeights.push(String.fromCharCode('z'.charCodeAt() - countLen % 26));
79+
}
80+
return sumWeights.join("")
81+
}
5882
let list = ["abcd","def","xyz"], weights = [5,3,12,14,1,2,3,2,10,6,6,9,7,8,7,10,8,9,6,9,9,8,3,7,7,2];
5983
/*
6084
Output: "rij"
@@ -64,4 +88,5 @@ The weight of "def" is 14 + 1 + 2 = 17. The result modulo 26 is 17 % 26 = 17, wh
6488
The weight of "xyz" is 7 + 7 + 2 = 16. The result modulo 26 is 16 % 26 = 16, which maps to 'j'.
6589
Thus, the string formed by concatenating the mapped characters is "rij".
6690
*/
67-
console.log(mapWordWeights(list,weights));
91+
console.log(mapWordWeights(list,weights));
92+
console.log(mapWordWeights2(list,weights));

javascript/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,3 +1279,27 @@ Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb").
12791279
// console.log(minimumDeletions(s));
12801280

12811281

1282+
var mapWordWeights = function(words, weights) {
1283+
let sumWeights = [];
1284+
1285+
for (const element of words) {
1286+
let countLen = 0;
1287+
for(let i = 0;i < element.length;++i) {
1288+
countLen += weights[element.charCodeAt(i) - 'a'.charCodeAt()]
1289+
}
1290+
// String.fromCharCode(ascii code) => ascii code to char.
1291+
sumWeights.push(String.fromCharCode('z'.charCodeAt() - countLen % 26));
1292+
}
1293+
return sumWeights.join("")
1294+
1295+
};
1296+
let list = ["abcd","def","xyz"], weights = [5,3,12,14,1,2,3,2,10,6,6,9,7,8,7,10,8,9,6,9,9,8,3,7,7,2];
1297+
/*
1298+
Output: "rij"
1299+
Explanation:
1300+
The weight of "abcd" is 5 + 3 + 12 + 14 = 34. The result modulo 26 is 34 % 26 = 8, which maps to 'r'.
1301+
The weight of "def" is 14 + 1 + 2 = 17. The result modulo 26 is 17 % 26 = 17, which maps to 'i'.
1302+
The weight of "xyz" is 7 + 7 + 2 = 16. The result modulo 26 is 16 % 26 = 16, which maps to 'j'.
1303+
Thus, the string formed by concatenating the mapped characters is "rij".
1304+
*/
1305+
console.log(mapWordWeights(list,weights));

0 commit comments

Comments
 (0)