/**
* @param {string} s
* @return {number}
*/
var maxPower = function(s) {
let count = 1;
let res = 1;
let arr = s.split("");
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] == arr[i + 1]) {
count++;
} else {
res = Math.max(count, res);
count = 1;
}
}
return res = Math.max(count, res);
};
/**
* @param {number[]} score
* @return {string[]}
*/
var findRelativeRanks = function(score) {
let answer = new Array(score.length).fill(0);
let tempArray = [...score];
tempArray.sort((a, b) => {
return b - a;
})
for (let i = 0; i < score.length; i++) {
if (tempArray.indexOf(score[i]) === 0) {
answer[i] = "Gold Medal";
} else if (tempArray.indexOf(score[i]) === 1) {
answer[i] = "Silver Medal";
} else if (tempArray.indexOf(score[i]) === 2) {
answer[i] = "Bronze Medal";
} else {
answer[i] = (tempArray.indexOf(score[i]) + 1) + "";
}
}
return answer
};
1005. K 次取反后最大化的数组和
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var largestSumAfterKNegations = function(nums, k) {
for (let i = 0; i < k; i++) {
nums.sort((a, b) => {
return a - b;
})
nums[0] = -nums[0];
}
let sum = 0;
nums.forEach(item => {
sum += item;
});
return sum;
};
383. 赎金信
/**
* @param {string} ransomNote
* @param {string} magazine
* @return {boolean}
*/
var canConstruct = function(ransomNote, magazine) {
let arr = magazine.split("");
let res = false;
arr.forEach(item => {
if (ransomNote.indexOf(item) > -1) {
ransomNote = ransomNote.replace(item, "");
}
})
if (ransomNote == "") {
res = true;
}
return res;
};
1816. 截断句子
/**
* @param {string} s
* @param {number} k
* @return {string}
*/
var truncateSentence = function(s, k) {
return s.split(" ").slice(0, k).join(" ");
};
748. 最短补全词
/**
* @param {string} licensePlate
* @param {string[]} words
* @return {string}
*/
var shortestCompletingWord = function(licensePlate, words) {
licensePlate = licensePlate.replace(/[0-9]/g, "");
licensePlate = licensePlate.replace(" ", "");
licensePlate = licensePlate.toLocaleLowerCase();
let minCount = 0;
let arr = licensePlate.split("");
let res = "";
words.forEach(item => {
let temp = item;
let flag = true;
arr.forEach(it => {
if (temp.indexOf(it) > -1) {
temp = temp.replace(it, "");
} else {
flag = false
}
})
if (flag) {
if (minCount == 0) {
minCount = item.length;
res = item
} else {
if (item.length < minCount) {
res = item
}
minCount = Math.min(minCount, item.length);
}
}
})
return res;
};