Title Description ~ Check if all characters appear the same number of times

Give you a string s , if s is a string, please return true , otherwise return false .

If all occurrences of s in 字符的出现次数 are the same, then we call the string s a string.

Example 1:

 输入:s = "abacbc"
输出:true
解释:s 中出现过的字符为 'a','b' 和 'c' 。s 中所有字符均出现 2 次。

Example 2:

 输入:s = "aaabb"
输出:false
解释:s 中出现过的字符为 'a' 和 'b' 。
'a' 出现了 3 次,'b' 出现了 2 次,两者出现次数不同。
Leetcode original title address: https://leetcode.cn/problems/check-if-all-characters-have-equal-number-of-occurrences

solution code

 var areOccurrencesEqual = function (s) {
    // 依然是熟悉的map集合统计次数
    let map = new Map()
    for (let i = 0; i < s.length; i++) {
        let item = s[i]
        if (map.has(item)) {
            let count = map.get(item)
            count = count + 1
            map.set(item, count)
        } else {
            map.set(item, 1)
        }
    }
    // 次数统计好了以后,再看是不是map集合中的每一项value都相等
    let flag = true // 假设每一项都相等
    let val = null // 定义一个初始值为null,后续用于比较
    for (const [key, value] of map) { // 使用forof遍历map集合
        // 这个if是初始去赋值,取第一项的value的值
        if (val == null) {
            val = value
        }
        // 这个是后续所有项的,再看后续项是否和第一项相等
        else {
            if (val != value) { // 只要有一个不相等就返回false,即不用继续比较了
                return false
            }
        }
    }
    // 若操作了一波没有变化,即为true
    return flag
};

Submit the result map

Topic Description ~ Sort Sentences

A 句子 refers to a sequence of words joined by a single space without any spaces at the beginning and end. Each word contains only lowercase or uppercase English letters.

We can add 从 1 the starting word position index to a sentence, and put all words in the sentence 打乱顺序 .

For example, the sentence "This is a sentence" can be shuffled to get "sentence4 a3 is2 This1" or "is2 sentence4 This1 a3" .
Give you a sentence 打乱顺序 s , which contains no more than 9 words, please reconstruct and get the sentence in the original order.

Example 1:

 输入:s = "is2 sentence4 This1 a3"
输出:"This is a sentence"
解释:将 s 中的单词按照初始位置排序,得到 "This1 is2 a3 sentence4" ,然后删除数字。

Example 2:

 输入:s = "Myself2 Me1 I4 and3"
输出:"Me Myself and I"
解释:将 s 中的单词按照初始位置排序,得到 "Me1 Myself2 and3 I4" ,然后删除数字。

Solution code one

 var sortSentence = function (s) {
    let sArr = s.split(' ') // 首先把字符串转成数组,因为要排序
    sArr.sort((a, b) => { // 注意这里的排序规则是最后一个字符,如Myself2 即2
        return a.at(-1) - b.at(-1) // 这样的话,就可以按照从1~9排序了
    })
    let res = "" // 准备一个变量字符串用于存储结果
    for (let i = 0; i < sArr.length; i++) { // 循环拼接得到结果
        // 注意,这里使用slice方法截取一下,因为字符串最后一个字符是数字,是不要的
        res = res + ' ' + sArr[i].slice(0, -1) 
    }
    return res.trimStart() // 最后再去掉左侧空格返回即可
};

Submit the result Figure 1

Solution code 2

In fact, it is not another solution. It is just that the above loop splicing is replaced by a reduce function. After this operation, we will find that the performance is slightly improved. Therefore, in general, the reduce function is preferred for operations

 var sortSentence = function (s) {
    let sArr = s.split(' ') // 转数组
    sArr.sort((a, b) => { // 按照单词最后一个数字排序
        return a.at(-1) - b.at(-1)
    })
    let res = sArr.reduce((temp, item) => { // reduce加工
        return temp + " " + item.slice(0, -1) // 字符串最后的一个数字字符不要
    }, '')
    return res.trimStart() // 去掉左侧空格
};

Submit the result Figure 2

Comparing the two submitted pictures, we found that the reduce function seems to have a little better performance.


水冗水孚
1.1k 声望588 粉丝

每一个不曾起舞的日子,都是对生命的辜负