Skip to content

Commit 8c7fcc3

Browse files
committed
practice to solve no.3838
1 parent 7cb00fb commit 8c7fcc3

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

javascript/index.js

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

12811281

1282+
/**
1283+
* 3838. Weighted Word Mapping
1284+
*
1285+
* 0 = z;1 = y;2 = x....;25 = a,26個字母倒著
1286+
*
1287+
* @param {string[]} words
1288+
* @param {number[]} weights
1289+
* @return {string}
1290+
*/
1291+
var mapWordWeights = function(words, weights) {
1292+
// 可能遇到的狀況:key(字母)、value(數字)重複出現
1293+
// modulo 26
1294+
let alp = generateAlphabet();
1295+
// console.log(alp)
1296+
// let mapWeights = new Map();
1297+
let sumWeights = [];
1298+
for(let i = 0;i < words.length;++i) {
1299+
let char = words[i].split("");
1300+
let countLen = 0;
1301+
let sum = 0;
1302+
for(let j = 0;j < weights.length;++j){
1303+
if(countLen !== char.length && sumWeights.length !== words.length){
1304+
sum += weights[j]
1305+
sumWeights.push(sum);
1306+
}
1307+
1308+
if(countLen === char.length){
1309+
countLen = 0;
1310+
sum = 0;
1311+
}
1312+
countLen++;
1313+
}
1314+
// for(let a = 0;a < char.length;++a) {
1315+
// // map{'a' => 5,'b' => 3,'c' => 12,'d' => 14,'d'=> 1}
1316+
// mapWeights.set(char[a],weights[a]);
1317+
// // j++;
1318+
1319+
// }
1320+
}
1321+
console.log(sumWeights)
1322+
1323+
1324+
/**
1325+
* 產生26個英文字母
1326+
* a = 26,b = 25 ....
1327+
* @returns obj
1328+
*/
1329+
function generateAlphabet(){
1330+
let start = "a";
1331+
let end = "z";
1332+
let alp = new Map();
1333+
let range = 25;
1334+
let i = start.charCodeAt(0), j = end.charCodeAt(0);
1335+
for (; i <= j; ++i) {
1336+
alp.set(String.fromCharCode(i),range--);
1337+
}
1338+
return alp;
1339+
}
1340+
};
1341+
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];
1342+
/*
1343+
Output: "rij"
1344+
Explanation:
1345+
The weight of "abcd" is 5 + 3 + 12 + 14 = 34. The result modulo 26 is 34 % 26 = 8, which maps to 'r'.
1346+
The weight of "def" is 14 + 1 + 2 = 17. The result modulo 26 is 17 % 26 = 17, which maps to 'i'.
1347+
The weight of "xyz" is 7 + 7 + 2 = 16. The result modulo 26 is 16 % 26 = 16, which maps to 'j'.
1348+
Thus, the string formed by concatenating the mapped characters is "rij".
1349+
*/
1350+
console.log(mapWordWeights(list,weights));

0 commit comments

Comments
 (0)