zoukankan      html  css  js  c++  java
  • LeetCode --- 字符串系列 --- 拼写单词

    拼写单词

    题目

    给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。

    假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),

    那么我们就认为你掌握了这个单词。


    注意:每次拼写时,chars 中的每个字母都只能用一次。
    返回词汇表 words 中你掌握的所有单词的 长度之和。


    示例

    示例 1:
    输入:words = ["cat","bt","hat","tree"], chars = "atach"
    输出:6
    解释: 
    可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。
    
    示例 2:
    
    输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
    输出:10
    解释:
    可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
    

    来源:力扣(LeetCode)

    链接:https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-charactersM

    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


    解题思路

    1、
    第一层循环
    循环 `字符串数组 words`,拿到 `单词字符串 word`
    2、
    第二层循环开始前,设置一个 `开关标志` ,一开始状态是 `true`
    第二层循环
    循环 `单词字符串 word` ,拿到 `单字符 w`
    3、
    第二层循环中
    在给出的 `字母表字符串 chars` 中查找是否存在该 `单字符 w`
    4、
    4、1
    如果不存在,则表示 `字母表字符串 chars` 中包含的字符组装不了当前的 `单词字符串 word` ,
    `开关标志` 设置成 `false`
    直接跳出第二层循环,继续第一层循环逻辑
    4、2
    如果存在,则去掉当前 `字母表字符串 chars` 中匹配到的字符,继续下一步第二层循环
    若直到第二层循环结束,开关标志都没有变化,则表示, `字母表字符串 chars` 包含了当前 `单词字符串 word`
    将当前单词字符串 word 的长度保存起来,继续第一层循环逻辑
    

    题解:

    let countCharacters = function(words, chars) {
        let l  = 0
        words.forEach(word => {
            // 循环 `字符串数组 words`,拿到 `单词字符串 word`
            let s = chars, judge = true
            for (let w of word) {
                // 循环 `单词字符串 word` ,拿到 `单字符 w`
                if (s.indexOf(w) === -1){
                    // 如果不存在,则表示 `字母表字符串 chars` 中包含的字符
                    // 组装不了当前的 `单词字符串 word` 
                    // `开关标志` 设置成 `false`
                    // 直接跳出第二层循环,继续第一层循环逻辑
                    judge = false
                    break
                }
                // 如果存在,则去掉当前 `字母表字符串 chars` 中匹配到的字符
                // 继续下一步第二层循环
                s = s.replace(w,'')
            }
            // 若直到第二层循环结束,开关标志都没有变化
            // 则表示, `字母表字符串 chars` 包含了当前 `单词字符串 
            if(judge) l += word.length
        })
        return l
    };
    

    解题渐进解题过程

    第一次提交:

    let countCharacters = function(words, chars) {
        let arr = []
        words.forEach(word => {
            let len = word.length
            let count = 0
            let splitChars = chars.split('')
            word.split('').forEach(char => {
                let index = splitChars.indexOf(char)
                if (index > -1) {
                    splitChars.splice(index, 1)
                    count++
                }
            })
            if (count === len) {
                arr.push(word)
            }
        })
        let l  = 0
        arr.forEach(item => {
            l += item.length
        })
        return l
    };
    

    第二次提交:

    let countCharacters = function(words, chars) {
        let l  = 0
        words.forEach(word => {
            let len = word.length
            let count = 0
            let splitChars = chars.split('')
            word.split('').forEach(char => {
                let index = splitChars.indexOf(char)
                if (index > -1) {
                    splitChars.splice(index, 1)
                    count++
                }
            })
            if (count === len) {
                l += word.length
            }
        })
        return l
    };
    

    第三次:

    let countCharacters = function(words, chars) {
        let l  = 0
        words.forEach(word => {
            let len = word.length
            let count = 0
            let splitChars = chars
            word.split('').forEach(char => {
                if (splitChars.indexOf(char) > -1) {
                    splitChars = splitChars.replace(char, '')
                    count++
                }
            })
            if (count === len) {
                l += word.length
            }
        })
        return l
    };
    

    第四次:

    let countCharacters = function(words, chars) {
        let l  = 0
        words.forEach(word => {
            let len = word.length
            let count = 0
            let splitChars = chars
            for (let w of word) {
                if (splitChars.indexOf(w) > -1) {
                    splitChars = splitChars.replace(w, '')
                    count++
                }
            }
            if (count === len) {
                l += word.length
            }
        })
        return l
    };
    

    第五次:

    let countCharacters = function(words, chars) {
        let l  = 0
        words.forEach(word => {
            let splitChars = chars
            let judge = true
            for (let w of word) {
                if (splitChars.indexOf(w) > -1) {
                    splitChars = splitChars.replace(w, '')
                } else {
                    judge = false
                    break
                }
            }
            if (judge) {
                l += word.length
            }
        })
        return l
    };
    

    第六次:

    let countCharacters = function(words, chars) {
        let l  = 0
        words.forEach(word => {
            let s = chars, judge = true
            for (let w of word) {
                if (s.indexOf(w) === -1){   
                    judge = false
                    break
                }
                s = s.replace(w,'')
            }
            if(judge) l += word.length
        })
        return l
    };
    
    

    2020-03-18 看到的比较好的力扣题解:

    备注:最终答案借鉴题解

    /**
     * @param {string[]} words
     * @param {string} chars
     * @return {number}
     */
    var countCharacters = function(words, chars) {
        let sum = 0
        for(let i=0 ; i<words.length; i++){
            const w = words[i]
            let s = chars, flag = true, j = 0
            while( j < w.length ){
                if( s.indexOf(w[j]) === -1 ){
                    flag = false
                    break
                }
                s = s.replace(w[j],'')
                j++
            }
            if( flag ) sum += w.length
        }
        return sum
    };
    
    都读到最后了、留下个建议如何
  • 相关阅读:
    NIO单一长连接——dubbo通信模型实现
    小谈网络游戏同步
    网络游戏同步问题综述
    TortoiseSVN客户端重新设置用户名和密码
    SVN服务器搭建和使用(三)
    SVN服务器搭建和使用(二)
    SVN服务器搭建和使用(一)
    Firefly 流程架构
    unity3d 手机震动
    Unity AssetBundle爬坑手记
  • 原文地址:https://www.cnblogs.com/linjunfu/p/12517333.html
Copyright © 2011-2022 走看看